Michdd Posted April 19, 2009 Share Posted April 19, 2009 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 More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 $string = preg_replace('~\[i:[^\]]*\]~i','<i>',$string); $string = preg_replace('~\[/i:[^\]]*\]~i','</i>',$string); to get your started Link to comment https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/#findComment-813549 Share on other sites More sharing options...
Michdd Posted April 19, 2009 Author Share Posted April 19, 2009 $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? Link to comment https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/#findComment-813552 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 yep. that would look for [list:randomstuffhere] and change it to <list> Link to comment https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/#findComment-813553 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 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] Link to comment https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/#findComment-813558 Share on other sites More sharing options...
Michdd Posted April 19, 2009 Author Share Posted April 19, 2009 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 * . That really helps A LOT. I've always been quite confused about that.. Link to comment https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/#findComment-813560 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 yes. * is a quantifier. It means match 0 or more of something. So if you want to look for a literal *, you are right, it must be escaped. Link to comment https://forums.phpfreaks.com/topic/154715-solved-working-on-strings/#findComment-813562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.