Jump to content

preg_match_all Questions


JustinK101

Recommended Posts

I am trying to do a preg_match_all and return any array of the items found.

 

Here is the pattern I am looking for:

 

id="{wildcard}@domain.com"

 

Here is the code I have:

 

$results = array();
preg_match_all('/id=\"*\@domain.com\"/', $html, $results);
print_r($result);

 

The output is: Array ( [0] => Array ( ) )

 

Does that mean it found a match, but how come the text is empty?

Link to comment
Share on other sites

<?php
$html = "&id=test@test.com  some extra stuff \n and id=teststst@test.com and eve some more \n stuff";
$results = array();
preg_match_all('~id=(.*?) ~', $html, $results);
echo "<pre>";
print_r($results);
die();
?>

 

Since you did not provide us with an example of what you want to parse, this will have to suffice, it requires that there be a space after the @domain.com to fetch the data.

 

Output of the above:

Array
(
    [0] => Array
        (
            [0] => id=test@test.com 
            [1] => id=teststst@test.com 
        )

    [1] => Array
        (
            [0] => test@test.com
            [1] => teststst@test.com
        )

)

Link to comment
Share on other sites

So is there a way to not require a space? Basically here is a context example of code:

 

<td class="cell truncate" truncate="80" id="justin@domain.com">justin@domain.com</td>

 

I am looking for id="{wildcard}@domain.com" should return: id="justin@domain.com" right?

Link to comment
Share on other sites

<?php
$html = '&id="test@test.com"  some extra stuff \n and id="teststst@test.com" and eve some more \n stuff';
$results = array();
preg_match_all('~id="(.*?)"~', $html, $results);
echo "<pre>";
print_r($results);
die();
?>

 

Output:

Array
(
    [0] => Array
        (
            [0] => id="test@test.com"
            [1] => id="teststst@test.com"
        )

    [1] => Array
        (
            [0] => test@test.com
            [1] => teststst@test.com
        )

)

Link to comment
Share on other sites

Premiso:

 

Close, but I don't think the regular expression is looking for @domain.com.

 

Misundestanding. I thought, when you said @domain.com it was a reference.

 

<?php
$html = '&id="test@domain.com"  some extra stuff \n test89@domain.com and id="teststst@domain.com" and id="test2@domain.com" and eve some more \n and id="test3@domain.com" stuff';
$results = array();
preg_match_all('~id="(.*?)@domain.com"~s', $html, $results);
echo "<pre>";
print_r($results);
die();
?>

 

Output:

Array
(
    [0] => Array
        (
            [0] => id="test@domain.com"
            [1] => id="teststst@domain.com"
            [2] => id="test2@domain.com"
            [3] => id="test3@domain.com"
        )

    [1] => Array
        (
            [0] => test
            [1] => teststst
            [2] => test2
            [3] => test3
        )

)

Link to comment
Share on other sites

$html = '&id="test@domain-x.com"  some extra stuff \n and id="teststst@domain.com" and eve some more \n stuff id="whatever"... &id="blah@domain.com"';
preg_match_all('#id="([^"]+@domain.com)"#', $html, $matches);
echo "<pre>".print_r($matches[1], true);

 

Output:

Array
(
    [0] => teststst@domain.com
    [1] => blah@domain.com
)

 

This assumes of course, that you absolutely want ...@domain.com (change this to match the actual domain in question of course). In order to ensure that I capture the domain and not some stuff in some id=".." that is not related, I use a negated character class..[^"]+ So the idea is to sacrifice speed for more accuracy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.