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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.