0

I have an example an HTML in a text file that contain

<div class="container">
  <div class="row" >
      <div class="col-lg-4" id="code-01"></div>
      <div class="col-lg-4" id="code-02"></div>
      <div class="col-lg-4" id="code-03"></div>
  </div>
</div>

I want to grab pieces of string out of that - anything that match

id="code-**"

If I do it right, it should print out. It should find it 3 times which is

  • id="code-01"
  • id="code-02"
  • id="code-03"

I've tried

I've treated it as a string for testing.

PHP

$string = '<div class="container"> <div class="row" > <div class="col-lg-4" id="code-01"></div> <div class="col-lg-4" id="code-02"></div> <div class="col-lg-4" id="code-03"></div> </div> </div>';


// dd(preg_match('/id="code-[0-9]"/', $string)); // I got 0
// dd(preg_match('/id="code-"/', $string)); // I got 0
// dd(preg_match('/id=/', $string)); // I got 1, why 1, there're 3 of them

JS

var string = '<div class="container"> <div class="row" > <div class="col-lg-4" id="code-01"></div> <div class="col-lg-4" id="code-02"></div> <div class="col-lg-4" id="code-03"></div> </div> </div>';

string.match(/id="code-\[(\d+)\]\[(\d+)\]/);

Can someone please give a little hint here ?

Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    How about [__`attribute-starts-with`__](https://api.jquery.com/attribute-starts-with-selector/) selector if you are using `jQuery` – Rayon Sep 21 '16 at 20:34
  • Beside from your pattern being not correct is regexp not made for HTML, use a DOM parser instead for much more reliable results. – Honk der Hase Sep 21 '16 at 20:35
  • @Rayon : That is a really cool one. I might use it. :D – code-8 Sep 21 '16 at 20:36
  • May be a dupe : [__`jquery` selector for id starts with specific text__](http://stackoverflow.com/questions/23223526/jquery-selector-for-id-starts-with-specific-text) – Rayon Sep 21 '16 at 20:38
  • 1
    Possible duplicate of [PHP DOMDocument getting Attribute of Tag](http://stackoverflow.com/questions/1597746/php-domdocument-getting-attribute-of-tag) – Sherif Sep 21 '16 at 20:40

4 Answers4

3

Would be much easier with jQuery:

$("div#[id^='code-']").each(function () {

    console.log('id=' + $(this).attr("id"));

});
2

The reason your PHP example is only returning 0 or 1 is that preg_match simply checks whether or not the string matches once. Try preg_match_all instead, as it will continue on and find all of the matches you are looking for. To retrieve the matched values, pass in the optional $matches argument and then review the contents of that array to find your results.

Dana Cartwright
  • 1,566
  • 17
  • 26
  • dd(preg_match_all('/id="code/', $string)); // return 3 now. Thanks. – code-8 Sep 21 '16 at 20:39
  • But how do `dd()` out the `id="code-01" id="code-02" id="code-03"` ? ALL the one that match ... u know what I mean .. – code-8 Sep 21 '16 at 20:40
  • 1
    It's in the docs: http://php.net/manual/en/function.preg-match-all.php. Specifically, you need to give it a `$matches` array to fill. – Dana Cartwright Sep 21 '16 at 20:41
  • @DanaCartwright : I tried `dd(preg_match_all('/id="code/', $string,$matches, PREG_PATTERN_ORDER));` , I still got 3.. :( – code-8 Sep 21 '16 at 20:45
  • 1
    @Sherif I did, in fact, read the documentation on those functions. `preg_match` returns a single match, with multiple capture groups, while `preg_match_all` keeps searching, filling a multi-dimensional array with captures from all matches. OP wants the latter. I didn't feel the need to expound on my answer since I'm sure "how to use `preg_match_all`" would be a duplicate. – Dana Cartwright Sep 21 '16 at 20:45
  • 1
    @ihue - the capture results will show up in the `$matches` array you pass in, not the return value of the function – Dana Cartwright Sep 21 '16 at 20:46
2

I have changed your regex for the following /id=["']code-[0-9]+/gi. Now its working.

PHP

$string = '<div class="container"> <div class="row" > <div class="col-lg-4" id="code-01"></div> <div class="col-lg-4" id="code-02"></div> <div class="col-lg-4" id="code-03"></div> </div> </div>';

preg_match('/id=["\']code-[0-9]+["\']/gi', $string));

JS

var string = '<div class="container"> <div class="row" > <div class="col-lg-4" id="code-01"></div> <div class="col-lg-4" id="code-02"></div> <div class="col-lg-4" id="code-03"></div> </div> </div>';

string.match(/id=["']code-[0-9]+["']/gi);
Maxime Gélinas
  • 2,202
  • 2
  • 18
  • 35
1

You can do it in PHP with strpos() or with preg_match_all():

$pos = 0;
$token = "code-";
$pattern = '/id="code-\d+"/';
$string = '<div class="container"> <div class="row" > <div class="col-lg-4" id="code-01"></div> <div class="col-lg-4" id="code-02"></div> <div class="col-lg-4" id="code-03"></div> </div> </div>';
$pos = strpos ( $string, $token);
while ( $pos !== false ) {
    echo substr ( $string, $pos, 7 ) . "->$pos<br>";
    $pos = strpos ( $string, $token, $pos+1 );
}
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);
var_dump($matches);

Using strpos() is less flexible since it expects code-??, so extra checks would be required.

Dominique Lorre
  • 1,168
  • 1
  • 10
  • 19