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
https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/
Share on other sites

<?php
$html = "&[email protected]  some extra stuff \n and [email protected] 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] => [email protected] 
            [1] => [email protected] 
        )

    [1] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
        )

)

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="[email protected]">[email protected]</td>

 

I am looking for id="{wildcard}@domain.com" should return: id="[email protected]" right?

<?php
$html = '&id="[email protected]"  some extra stuff \n and id="[email protected]" 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="[email protected]"
            [1] => id="[email protected]"
        )

    [1] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
        )

)

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="[email protected]"  some extra stuff \n [email protected] and id="[email protected]" and id="[email protected]" and eve some more \n and id="[email protected]" stuff';
$results = array();
preg_match_all('~id="(.*?)@domain.com"~s', $html, $results);
echo "<pre>";
print_r($results);
die();
?>

 

Output:

Array
(
    [0] => Array
        (
            [0] => id="[email protected]"
            [1] => id="[email protected]"
            [2] => id="[email protected]"
            [3] => id="[email protected]"
        )

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

)

$html = '&id="[email protected]"  some extra stuff \n and id="[email protected]" and eve some more \n stuff id="whatever"... &id="[email protected]"';
preg_match_all('#id="([^"][email protected])"#', $html, $matches);
echo "<pre>".print_r($matches[1], true);

 

Output:

Array
(
    [0] => [email protected]
    [1] => [email protected]
)

 

This assumes of course, that you absolutely want [email protected] (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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.