Jump to content

REGEX help


sanjoyc

Recommended Posts

Please help me in REGEX code. I am very new to REGEX. I have text like:
[code]This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...[/code]
I need to replace all [b]{{img[/b] with [b]<img[/b] and [b]}}[/b] with [b]>[/b]

How could I do this? Anyone can help me?
Link to comment
https://forums.phpfreaks.com/topic/18637-regex-help/
Share on other sites

[quote author=sanjoyc link=topic=105599.msg421884#msg421884 date=1156506742]
Please help me in REGEX code. I am very new to REGEX. I have text like:
[code]This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...[/code]
I need to replace all [b]{{img[/b] with [b]<img[/b] and [b]}}[/b] with [b]>[/b]

How could I do this? Anyone can help me?

[/quote]

try this:
[code]
<?php
$String = preg_replace('|\{\{img(.+?)\}\}|i', "<img$1 />", $String;
?>
[/code]

hope this helps
Link to comment
https://forums.phpfreaks.com/topic/18637-regex-help/#findComment-80300
Share on other sites

Thanks for quick reply.

But my problem does not solve yet.

The text [b]{{img=[/b] should be replaced with [b]<img src[/b] and [b]}}[/b] with [b]>[/b]
So the following text
[quote]This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...[/quote]
should be look like
[quote]This is an image <img src='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'> and this is another image <img src=='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'>...blah blah blah...[/quote]

But when I used your code:
[quote]$str = "This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...";

$test = preg_replace('|\{\{img(.+?)\}\}|i', "<img$1 />", $str);

print_r( $test);[/quote]
I got the result like [quote]This is an image  and this is another image ...blah blah blah...[/quote]

I think, I can clarify my problem now.

Thanks
Link to comment
https://forums.phpfreaks.com/topic/18637-regex-help/#findComment-80308
Share on other sites

actually, if you view your source of your page instead of just the output, you'll see that the code provided does exactly what you're after. the problem is that when you export it to the screen, the browser will interpret it as an image tag. if you're wanting to print the result to the screen, use htmlentities() on it first. try this:
[code]
<?php
$str = "This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...";

$test = htmlentities(preg_replace('|\{\{img(.+?)\}\}|i', "<img$1 />", $str));

print_r( $test);
?>
[/code]

the result of that code is:
[quote]
This is an image <img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text' /> and this is another image <img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text' />...blah blah blah...
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/18637-regex-help/#findComment-80317
Share on other sites

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.