The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
2nd pair of eyes
Discuss 2nd pair of eyes in the Regex Programming forum on Dev Shed. 2nd pair of eyes Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 18th, 2009, 12:06 AM
|
|
Contributing User
|
|
Join Date: Aug 2004
Posts: 70
Time spent in forums: 12 h 13 m 51 sec
Reputation Power: 9
|
|
|
2nd pair of eyes
For what ever reason this is not working as far as i can tell it should be. Im just Trying to get everything between the specific <td class=\"back_build\"> tags. I told it to ignore the spaing with the \s* but it returns no results. Need a second pair of eyes to see what I may be missing.
Code:
<?
$string = " <td class=\"back_build\">
<font style=\"font-weight:bold;font-size:13px;\"></font>
<font class=\"cost_text\" style=\"font-size:11px;\">
<img src='images/pic5.png' align='absmiddle' alt='num1' title='num1'/> 92363 </font><br />
<font class=\"cost_text\" style=\"font-size:11px;\">
<img src='images/pic7.png' align='absmiddle' alt='num2' title='num2'/> 323270 </font><br />
<font class=\"cost_text\" style=\"font-size:11px;\">
<img src='images/pic6.png' align='absmiddle' alt='num3' title='num3'/> 129308 </font><br />
</td>
";
preg_match_all("/<td class=\"back_build\">\s*(.*?)\s*<\/td>/", $string, $matches);
print_r ($matches);
?>
|

August 18th, 2009, 01:57 AM
|
|
|
Try this:
Code:
preg_match_all("/<td class=\"back_build\">\s*(.*)\s*<\/td>/s", $string, $matches);
|

August 18th, 2009, 02:38 AM
|
|
Contributing User
|
|
Join Date: Aug 2004
Posts: 70
Time spent in forums: 12 h 13 m 51 sec
Reputation Power: 9
|
|
|
Ahh the 'S' at the end.. thank you so much RuleMaker i figured it was something simple.
|

August 18th, 2009, 02:44 AM
|
|
|
|
No problem, just in case somebody doesn't know, the "s" flag indicates the multi-line search.
|

August 18th, 2009, 05:49 AM
|
|
|
Quote: | Originally Posted by RuleMaker No problem, just in case somebody doesn't know, the "s" flag indicates the multi-line search. |
No, it does not. The 's' flag is also called the DOT-ALL flag. The 'm' is called the MULTI-LINE flag.
|

August 18th, 2009, 05:53 AM
|
|
|
Quote: | Originally Posted by RuleMaker Try this:
Code:
preg_match_all("/<td class=\"back_build\">\s*(.*)\s*<\/td>/s", $string, $matches);
|
Especially with the s-flag, the greedy DOT-STAR should be avoided (in this case). I'd advice the OP to at least make it reluctant as s/he already had in his/her original post:
PHP Code:
"/<td class=\"back_build\">\s*(.*?)\s*<\/td>/s"
Or (IMO) better yet, do:
PHP Code:
"/<td\s+class=\"back_build\">\s*([^<]*)\s*<\/td>/" // no s-flag needed
Last edited by prometheuzz : August 18th, 2009 at 06:15 AM.
|

August 18th, 2009, 05:58 AM
|
|
|
Quote: | Originally Posted by prometheuzz No, it does not. The 's' flag is also called the DOT-ALL flag. The 'm' is called the MULTI-LINE flag. |
We are talking about php, could you explain why it doesn't return any match with "m" flag while it returns exactly what it's expected with the "s" flag?
|

August 18th, 2009, 06:14 AM
|
|
|
Quote: | Originally Posted by RuleMaker We are talking about php, could you explain why it doesn't return any match with "m" flag while it returns exactly what it's expected with the "s" flag? |
I didn't say it would return a match using the MULTI_LINE flag ('m'). You just used the wrong terminology: the 's' is called the DOT-ALL flag which will let the DOT match line terminators as well (hence: DOT-ALL).
|

August 18th, 2009, 06:25 AM
|
|
|
Quote: | Originally Posted by prometheuzz I didn't say it would return a match using the MULTI_LINE flag ('m'). You just used the wrong terminology: the 's' is called the DOT-ALL flag which will let the DOT match line terminators as well (hence: DOT-ALL). |
Thanks, I always thought "s" was the newline since it does match newlines, but still I'd like to know why the "m" flag, in this case, doesn't return any match even though the string is multiline.
|

August 18th, 2009, 06:33 AM
|
|
|
Quote: | Originally Posted by RuleMaker Thanks, I always thought "s" was the newline since it does match newlines, but still I'd like to know why the "m" flag, in this case, doesn't return any match even though the string is multiline. |
When using the 'm' flag, the '^' not only matches the start of the entire input string, but also matches the beginning of each line in the string. And the '$' not only matches the end of the input string, but also the and of each line.
Example input:
Now let's say you want to match only the numbers that are at the end of each line, you can do:
PHP Code:
$input = '12 34
56 78
90 00';
preg_match_all('/\d+$/m', $input, $matches);
print_r($matches);
which will produce the following output:
Code:
Array
(
[0] => Array
(
[0] => 34
[1] => 78
[2] => 00
)
)
And when removing the 'm' flag in my example, you will see that only the "00" is matched.
|

August 18th, 2009, 06:45 AM
|
|
|
|
Thanks!
|

August 18th, 2009, 06:48 AM
|
|
|
Quote: | Originally Posted by RuleMaker Thanks! |
You're welcome.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|