sanjoyc Posted August 25, 2006 Share Posted August 25, 2006 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 Link to comment Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 [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 Quote Link to comment Share on other sites More sharing options...
sanjoyc Posted August 25, 2006 Author Share Posted August 25, 2006 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 Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 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] Quote Link to comment Share on other sites More sharing options...
sanjoyc Posted August 28, 2006 Author Share Posted August 28, 2006 Many many thanks.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.