Myself and my team are stuck on this one, I have the following code.
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut bibendum augue eu arcu mollis cursus. Curabitur nec purus ipsum. Fusce ut erat leo, vitae malesuada lacus. Quisque varius gravida justo ut aliquam. Integer accumsan, ante non volutpat semper, orci sem luctus odio, sit amet convallis odio justo id nisl. Nunc sed lacus nisi, quis accumsan massa. Donec ante enim, fermentum sit amet placerat nec, eleifend elementum nibh
[[BLOGIMAGE_20090303011757.jpg||480]]
us dolor nec est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam accumsan blandit purus eget vestibulum. Nullam neque sem, suscipit sit amet mattis eu, imperdiet quis ligula. Integer aliquam dapibus gravida. Pellentesque ultrices sapien orci. Suspendisse at eros non dolor accumsan cursus mattis nec justo.
[[BLOGIMAGE_20090303011819.jpg||480]]
Aenean cursus lacinia arcu vitae malesuada. Fusce fermentum enim sit amet elit fermentum at consectetur ante vulputate. Aliquam sagittis nulla id magna facilisis tempus. Suspendisse eget feugiat libero. Pellentesque non lorem sem, eu posuere velit. Nulla id nulla ligula.
[[BLOGIMAGE_20090303011842.jpg||480]] ..... etc";
$pat = "\[\[(.*)\|\|(.*)\]\]";
$mat = '<img src="/path/to/file/imgs/$1" width="$2px" />';
$text = preg_replace($pat , $mat, $text);
What we want to do is convert the [[imagefile||size]] into the image tag using the structure in the $mat. The match works perfectly in RegExr (an adobe air program), and verious javascript online testers
The error I'm getting is :-
Message: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash
So if I add a dilimiter, such as...
$pat = "^[[(.)\|\|(.)]]^";
Then the match up stops working but I no longer get any errors.
Any help really would be appreciated.
preg_replace()
requires a delimiter character:
preg_replace("/$pat/" ...
Traditionally it's the forward slash, but it can be any character - especially when you need the forward slash in the regex itself you can resort to another character.
This flexibility allows you to express "/http:\/\/foo\/bar\//"
("leaning toothpick syndrome") as "!http://foo/bar/!"
.
The delimiter character is necessary to separate the regex from the regex flags (a.k.a. "modifiers"), for example:
preg_replace("/$pat/i" ...
…this uses the i
flag to declare a case-insensitive regex.
From the PHP manual on PCRE delimiters:
A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Often used delimiters are forward slashes (
/
), hash signs (#
) and tildes (~
).
So you could use /
as delimiter to separate the pattern from optional modifiers:
/\[\[(.*)\|\|(.*)\]\]/
But also note:
In addition to the aforementioned delimiters, it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively.
Furthermore, currently your pattern will match as much as possible as both quantifiers are greedy; you might want to change them to be reluctant to only match as little as possible:
/\[\[(.*?)\|\|(.*?)\]\]/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With