Jump to content

[SOLVED] Manipulating Only Certain Parts Of A String? Help Greatly Appreciated!


steven fullman

Recommended Posts

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  :confused:

 

You help would be greatly appreciated!

 

Kind regards,

Steve

 

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;
?>

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.

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

<?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.

Daniel0,

 

This is perfect! Amazing...I spent so much time trying to figure it out...and you did it in 6 lines... :o

 

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.

Archived

This topic is now archived and is closed to further replies.

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