Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stripping string from variable 1

Status
Not open for further replies.
Jul 28, 2005
358
FR
HI,

I am using tiny_mce wysywig editor to allow a client to change pages on their site. I store the page content in a mysql database, but because of a few issues I have with tinymce I have had to stop it cleaning up the html it produces. Because of this it is inserting some propriety code which screws up my html validation. This is all to do with temporary image and link paths the script uses to display them in the editor.

What I would like to do is have php parse the html and strip them out. They are in the form of
Code:
mce_href="Full_List.htm"
and 
mce_src="images/Rentals1.gif"

How can I get php to search for these instances and delete them from the string, baring in mind that the content inside of the speech marks is going to be different for each instance.

Thanks,

Richard
 

If you need to strip out a given tag from a string, this function might help you:

Code:
function strip_weird_code($string,$weirdo)
 {
 $lenght = strlen($string);

 $part_2 = strstr($string,$weirdo);
 $part_2_lenght = strlen($part_2);

 $part_1_lenght = $lenght-$part_2_lenght;
 $part_1 = substr($string,0,$part_1_lenght);
 
 $weirdo_lenght = strlen($weirdo);
 $new_part_2 = substr($part_2,$weirdo_lenght+2);

 $new_part_2 = strstr($new_part_2,'"');
 $new_part_2 = substr($new_part_2,1);

 return $part_1.$new_part_2;
 }

Hope it helps...

 
Thanks for the function bu I can't seem to get it working.

Can you possibly give an example of it's usage in relation to my problem?

Thanks a lot

Richard
 
Usage strip_weird_code($string,$weirdo)

Code:
$string = 'bla bla bla mce_href="Full_List.htm" bla bla bla';

$weirdo = 'mce_href';

strip_weird_code($string,$weirdo);

// returns 'bla bla bla bla bla bla'

Hope it helps...

 
ahh, right. Will this do it for all instance of the string or just one.

I suppose I also need to work out how to get it to strip out when the stuff between the speech marks is different for each instance.

Richard
 
The function will strip out the weird attribute and the stuff between speech marks, whatever that stuff is. It will only strip out the first instance of $weirdo it founds on the string.

If you want to check for all instances you could insert a while loop searching for strstr($weirdo) at the beginning of the function, so it will repeat the process until it doesn't find any more instances.

It should be something like this:

Code:
function strip_weird_code($string,$weirdo)
 {
 while(strstr($string,$weirdo))
  {
  $lenght = strlen($string);

  $part_2 = strstr($string,$weirdo);
  $part_2_lenght = strlen($part_2);

  $part_1_lenght = $lenght-$part_2_lenght;
  $part_1 = substr($string,0,$part_1_lenght);

  $weirdo_lenght = strlen($weirdo);
  $new_part_2 = substr($part_2,$weirdo_lenght+2);

  $new_part_2 = strstr($new_part_2,'"');
  $new_part_2 = substr($new_part_2,1);

  $string = $part_1.$new_part_2;
  }

 return $string;
 }

 
Brilliant,

Thanks for that. Should have noticed from your previous post that it stripped the content of the url too.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top