Jump to content

Archived

This topic is now archived and is closed to further replies.

sanjoyc

REGEX help

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?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.