Jump to content

Need Regular Expression For This!!


gaza165

Recommended Posts

Something tells me there is more to it than simply looking for the above. You want to look for something similar to the above.. in what way?

Here is a sample that matches that anchor exactly: Damn, CV beat me to it!  :tease-03:

 

So we skip the pattern and move on to...

 

I'm not sure what you mean by something similar... is there some parts of the url that are always going to change? Is there only one specific anchor tag you seek, or is there multiple ones? When dealing with regex, you have to be crystal clear as to what you are looking for, as regex is a very terse system that needs to be precisely thought out in order for it to work.

Link to comment
Share on other sites

Well basically what I am trying to do is...

 

On my shoutbox, users are able to use bbcodes to add an image to the shoutbox!! like this

 

[ img ]http://www.domain.com/image.png [/ img ]

 

when it is displayed, i only have the thumbnail displayed, what i want to do is encase any images that posted with its url so the user can click the thumbnail to navigate to the large image.

 

So the regex is to find this image and encase its <a> around it...

 

What is the best way to go about this?? Am i looking at it the wrong way???

Link to comment
Share on other sites

So if I understand you correctly, is this along the lines of what you are looking for?

 

Example:

$str = '[img=http://www.domain.com/image.png]';
$str = preg_replace('#([ img ]([^[]+)[ /img ])#i', '<a href="$2">$1</a>', $str);
echo $str; // <a href="[url=http://www.domain.com/image.png]http://www.domain.com/image.png[/url]">[img=http://www.domain.com/image.png]</a>

Note, due to the posting system trying to convert my ... tags in the pattern to an actual image, I inserted spaces.. but if you cut and paste this snippet to test it out, remove the spaces in the [ img ]  and [ /img ] part of the pattern. Note also that in the code comment, the posting system inserts the url bbc tags as well.. ignore those.

Link to comment
Share on other sites

what i need to replace it with is this....

 

 

 

Right, I see (I saw you mentioned <a> tag and wasn't thinking in context of forum bbc url tags.

 

Example:

$str = '[img=http://www.domain.com/image.png]';
$$str = preg_replace('#(\[ img \]([^[]+)\[ /img \])#i', '[ url=$2 ]$1[ /url ]', $str);
echo $str; // [url=http://www.domain.com/image.png][img=http://www.domain.com/image.png][/url]

Once again, remove the spaces in \[ img \], \[ /img \] and [ url=$2 ]$1[ /url ]

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.