TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   preg_replace limit results (http://www.talkphp.com/absolute-beginners/2607-preg_replace-limit-results.html)

oMIKEo 04-09-2008 10:02 AM

preg_replace limit results
 
Hi,

I want to extract the value between [SiteName] from $postText and save it as $s_sitename. Is preg_replace the best way to do this?

Here is what i have so far:

PHP Code:

$pattern[0] = "/\[SiteName\](.*)\[\/SiteName\]/";
$replace[0] = "$1";
$s_sitename preg_replace($pattern$replace$postText); 

At the moment i get the sitename but it then shows all the other text in $postText.

Thanks!

Wildhoney 04-09-2008 12:00 PM

PHP Code:

$postText 'Hello [SiteName]TalkPHP[/SiteName]!';
preg_match_all('~\[SiteName\](.+?)\[/SiteName\]~i'$postText$matches);

echo 
'<pre>';
print_r($matches);
echo 
'</pre>'


oMIKEo 04-09-2008 02:25 PM

Thanks Wildhoney,

Using your bit of code and 2 mins on php.net i found exactly what i needed :)

oMIKEo 11-12-2008 04:07 PM

Noticed a slight problem now, if there is a return in the middle of the 2 SiteNotes it doesnt display anything. Here is what i have so far:

PHP Code:

$postText $row["post_text"];
preg_match_all('~\[SiteNotes\](.+?)\[/SiteNotes\]~i'$postText$s_SiteNotes); 

Any ideas how to make it find matches even if there is a return in the middle?

Thanks,
Mike

Salathe 11-12-2008 04:11 PM

Add the s modifier after the i at the end of the pattern: ~\[SiteNotes\](.+?)\[/SiteNotes\]~is This turns on the PCRE_DOTALL flag so that . includes newlines.

oMIKEo 11-13-2008 10:38 AM

That worked perfectly - thank you.


All times are GMT. The time now is 03:03 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0