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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.