homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

REGEX and special characters

 4:05 pm on May 6, 2010 (gmt 0)

Hi guys,

I got some HTML with homemade tags and I would like to parse it and get an array of the result.

For example :

<h1>Hello World</h1>
{: foo :}
<p>some dummy text</p>
{: bar :}
<p>some dummy text</p>

My goal is to replace the {: foo :} and {: bar :} by some homemade php widgets.
Here is the code I'm using now, but it's not working :

$pattern = "/\{\: (.*) \:\}/";
if(preg_match_all($pattern,$snippet, $matches)){
foreach( $matches as $item )
$widgetname = $item[1] ;}

Let me know if I make myself clear... Any ideas would be more than welcome, I'm stuck !
Thanks in advance !



 4:18 pm on May 6, 2010 (gmt 0)

You don't need to escape colons... Try this:



 9:08 pm on May 6, 2010 (gmt 0)

That worked perfectly, thanks for that !
However, it returns the full {: foo :} and not only foo - how would you do that ?


 9:25 pm on May 6, 2010 (gmt 0)

Hi there Cirilo007,

Good to see as you have a solution, but I am curious to the use; are you using {:foo :} as a place holder, to be replaced with some content after the preg_match() has filtered the data?

Only reason I ask this question is because if you are using this as a place marker you can use str_replace(); to better effect IMO, there again, If I have missed the point, that's not the aim of the 'widget'

Or am I missing the point?



 9:31 pm on May 6, 2010 (gmt 0)

If you want to only grab the "foo"...


Back reference #1 (so with preg_match_all('/\{:\s([^\s]+)\s:\}/im', $input, $matches) $matches[1]) will contain "foo"


 9:50 pm on May 6, 2010 (gmt 0)

Sweet !
Thanks man :)


 10:01 pm on May 6, 2010 (gmt 0)

@Matthew1980 : what would be the benefits of str_replace against preg_match_all in this case ? I got the script working now, but if you got a better solution, I'm more than interested !


 10:04 pm on May 6, 2010 (gmt 0)

The string functions have less overhead, but you'd have to individually define every single possible pseudonym of "foo". Since I'm assuming there's several potential variations, the preg functions will not only make managing your code easier here, it'll also be more efficient than individual definitions.

preg_* => good for patterns
str_* => good for static strings


 10:16 pm on May 6, 2010 (gmt 0)

Hi there Cirilo007,

Check this link for more info [uk2.php.net ]

But from a example:-

$First = "ford";
$Second = "beanie";
$DataString = "I have a red car and a green hat";

$DataString = str_replace("red", $First, $DataString);
$DataString = str_replace("green", $Second, $DataString);

echo $DataString;

So far as your place holders would go you would do something like:-

str_replace("{:foo:}", $WhatYouWantToReplace, $YourText);

Hopefully from this example you can see the effect, I'm not saying its the absolute preferred way, but IMHO it's easier as you don't need regexp to sift through the data.

There are many ways of doing a few things with php, that's the beauty of learning new ways ;)

[EDIT] I agree with readie there, again just my preference if it is going to be used in conjunction with place holders.



 10:27 pm on May 6, 2010 (gmt 0)

Hey guys,

That's all the point. Foo has not only several potential variations, but you can also add variables to it like {: foo[bar] :} (that is what I'm working on now).

Using str_replace in this case would oblige me to define each foo, and that is not acceptable. But thanks for your ideas, always a pleasure to share !

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved