steven fullman Posted September 28, 2009 Share Posted September 28, 2009 Hi All, I've been scratching my head with this for a while...and then I stumbled across this excellent forum. So, apologies in advance that my first post here is a question :-\ And I hope you can help! OK, so I have a function that manipulates a given string...let's say for example it upper-case's it. No problem there, obviously. However, I want my users to be able to put "short code" around parts of the input string that will leave that portion of the string untouched. Here's what I mean. This would be the input string: This text should be converted to upper case. [leavealone]This text should remain the same[/leavealone]. This text should be upper case. [leavealone]Don't change the case of this part[/leavealone] etc... So, the end result should look like: THIS TEXT SHOULD BE CONVERTED TO UPPER CASE. This text should remain the same. THIS TEXT SHOULD BE UPPER CASE. Don't change the case of this part etc... In other words, ignore any text within the [leavealone]...[/leavealone] tags. What would be the easiest method to achieve this? preg_match? I've been hitting my head against the wall for ages with this one, and I'm getting nowhere You help would be greatly appreciated! Kind regards, Steve Quote Link to comment https://forums.phpfreaks.com/topic/175822-solved-manipulating-only-certain-parts-of-a-string-help-greatly-appreciated/ Share on other sites More sharing options...
redarrow Posted September 28, 2009 Share Posted September 28, 2009 little example....... it called bbcode bro. <?php $string = "I am so [up]cool[/up] "; $bb1= array("[up]","[/up]"); $bb2= array ('<div style="text-transform:uppercase";> ','</div>'); $string = str_replace($bb1,$bb2,$string); echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175822-solved-manipulating-only-certain-parts-of-a-string-help-greatly-appreciated/#findComment-926477 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 little example....... <?php $string = "I am so [up]cool[/up] "; $bb1= array("[up]","[/up]"); $bb2= array ('<div style="text-transform:uppercase";> ','</div>'); $string = str_replace($bb1,$bb2,$string); echo $string; ?> He wants to do the opposite though, instead of transforming what's inside the BBcode transform what's not inside of it. Quote Link to comment https://forums.phpfreaks.com/topic/175822-solved-manipulating-only-certain-parts-of-a-string-help-greatly-appreciated/#findComment-926478 Share on other sites More sharing options...
redarrow Posted September 28, 2009 Share Posted September 28, 2009 Just add lower case then? <?php $string = "I am so [leavealone]cool[/leavealone] "; $bb1= array("[leavealone]","[/leavealone]"); $bb2= array ('<div style="text-transform:lowercase;"> ','</div>'); $string = str_replace($bb1,$bb2,$string); echo $string; ?> it all getting to messy you don't do it that way .... you only let user's add bb to words needing to be altered. by default all words letters should be lowercase... Quote Link to comment https://forums.phpfreaks.com/topic/175822-solved-manipulating-only-certain-parts-of-a-string-help-greatly-appreciated/#findComment-926479 Share on other sites More sharing options...
Daniel0 Posted September 28, 2009 Share Posted September 28, 2009 <?php $string = "This text should be converted to upper case. [leavealone]This text should remain the same[/leavealone]. This text should be upper case. [leavealone]Don't change the case of this part[/leavealone] etc..."; $parts = preg_split('#\[/?leavealone\]#', $string); for ($i = 0, $m = count($parts); $i < $m; $i += 2) { $parts[$i] = strtoupper($parts[$i]); } $newString = join('', $parts); echo $newString; I believe the code is rather self-explanatory, but I'll explain it in details if you need it. Quote Link to comment https://forums.phpfreaks.com/topic/175822-solved-manipulating-only-certain-parts-of-a-string-help-greatly-appreciated/#findComment-926488 Share on other sites More sharing options...
steven fullman Posted September 28, 2009 Author Share Posted September 28, 2009 Daniel0, This is perfect! Amazing...I spent so much time trying to figure it out...and you did it in 6 lines... Absolutely brilliant. Thank you SO much. And thanks as well to everyone else who came to my rescue. This is a GREAT forum! My kindest regards, Steve <?php $string = "This text should be converted to upper case. [leavealone]This text should remain the same[/leavealone]. This text should be upper case. [leavealone]Don't change the case of this part[/leavealone] etc..."; $parts = preg_split('#\[/?leavealone\]#', $string); for ($i = 0, $m = count($parts); $i < $m; $i += 2) { $parts[$i] = strtoupper($parts[$i]); } $newString = join('', $parts); echo $newString; I believe the code is rather self-explanatory, but I'll explain it in details if you need it. Quote Link to comment https://forums.phpfreaks.com/topic/175822-solved-manipulating-only-certain-parts-of-a-string-help-greatly-appreciated/#findComment-926621 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.