Jump to content

Recommended Posts

I need help making something that will search through a string and change something like this:

 

[i:1pqdiwwk] to <i> and [/i:1pqdiwwk] to </i> Where there will be random things between ':' and ']'

 

And also, something like: [*:2rvk5vh9] to <li> [/*:2rvk5vh9] to </li> and [list:2rvk5vh9] to <ul>, [/list:2rvk5vh9] to </ul>

 

If you give me an example of how to do one, I can probably do the rest myself.

Link to comment
https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/
Share on other sites

$string = preg_replace('~\[i:[^\]]*\]~i','<i>',$string);
$string = preg_replace('~\[/i:[^\]]*\]~i','</i>',$string);

to get your started

That's a bit confusing.. I've looked up the guide on preg_replace() still a bit confused. :L Makes me feel stupid since I've been working with PHP so long and feel like I know it somewhat well..

 

Does this mean I could also do something like:

 

$string = preg_replace('~\[list:[^\]]*\]~i','<list>',$string);

 

? Or would the pattern have to be totally different?

That is regular expressions, or regex, for short.  There is a subforum to this forum dedicated to it, with stickies to tuts, books, etc...

 

basically that pattern says;

 

~\[i:[^\]]*\]~i

 

the pattern delimiter. tells the regex engine where the start and stop of the pattern is.

literal [ . that character is significant to the engine, so we have to escape it so engine knows to look for a literal [.

more of what you want the engine to match literally

basically says to keep matching everything until you find a ]

literal ]. the closing tag to literally match.

pattern modifier. means case-insensitive (so it will work with [i:blahblah] or [i:blahblah]

 

That is regular expressions, or regex, for short.  There is a subforum to this forum dedicated to it, with stickies to tuts, books, etc...

 

basically that pattern says;

 

~\[i:[^\]]*\]~i

 

the pattern delimiter. tells the regex engine where the start and stop of the pattern is.

literal [ . that character is significant to the engine, so we have to escape it so engine knows to look for a literal [.

more of what you want the engine to match literally

basically says to keep matching everything until you find a ]

literal ]. the closing tag to literally match.

pattern modifier. means case-insensitive (so it will work with [i:blahblah] or [i:blahblah]

 

Thanks! That really helped a lot, I was having a problem with the [*:stuff..] one, but with that help I knew that I just had to escape the * . :P That really helps A LOT. I've always been quite confused about that..

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.