Jump to content

[SOLVED] String Replace Question. Please Help


Alex C

Recommended Posts

Hi,

 

I hope I'm posting in the right place and forgive me if I'm not. I know theres a simple solution to my problem but i cant seem to find the right example from searching around.

 

Basically all i want to do is search a string for a particular point and then wipe over whatever is in the rest of the string from that point onwards. I know about str_replace() but the examples only show how to search for a word say and just replace that.

 

The string is dynamic in legnth so i cant just count in. I need to look for a flag maybe and wipe over data from there.

For example i want to do this:

 

$string = "Content i want to keep content i want to keep <flag> content to wipe over content to wipe over";

 

So basically i need to read the above string, find the <flag> then replace whatever is left in the string from that point onwards with my new content.

 

The string is always slightly different in length for both content i want and content i want to wipe over. I'm certain there is a simple solution to this but I'm slightly stuck at the mo and would really appreciate any help. I hope i have explained it enough.

 

Hope someone can shed some light.

 

Thanks,

Alex

u can do that a number of ways

<?php
$body = "Content i want to keep content i want to keep <flag> content to wipe over content to wipe over";
if(($found_flag=strpos($body,'<flag>'))!==FALSE)
   $body=substr($body,0,$found_flag) . "This is my new content ";
echo $body;   
?>

shud output

Content i want to keep content i want to keep This is my new content :)

You guys beat me to it. But still posting :)

 

If the flag shouldn't be included in the return string, this will work. (Else just append the flag). PHP 5 only:

 

<?php
$flag = 'flag!';
$string = '1 2 3 4 5 6 flag! a b c d e f';
$substring = strstr($string, $flag, true);
// $substring contains '1 2 3 4 5 6 '
?>

 

PHP 4 method:

 

<?php
$flag = 'flag!';
$string = '1 2 3 4 5 6 flag! a b c d e f';
$substring = substr($string, 0, strpos($string, $flag));
// $substring contains '1 2 3 4 5 6 '
?>

 

I'm sure you don't need help appending whatever you want to the substring.

Its almost perfect but i have just realized something. I'm using laffin's example

 

<?php
$body = "Content i want to keep content i want to keep <flag> content to wipe over content to wipe over";
if(($found_flag=strpos($body,'<flag>'))!==FALSE)
   $body=substr($body,0,$found_flag) . "This is my new content ";
echo $body;   
?>

 

Can it just replace after the <flag> and maintain the <flag> in tact because when i want to update the string again the <flag> is gone so obviously it wont work :( At the moment the above code looks for the <flag> and wipes over that tag too.

 

I just need it to re write after the <flag> tag so it keeps the <flag> in the string.

 

Hope its a simple answer I'm sure it is. You guys are geniuses!

 

Thanks

Lets put this all in a function.

 

<?php

  function replaceend($content, $replace, $flag) {
    if (($found_flag = strpos($content,$flag))!==FALSE)
      return substr($body,0,$found_flag+strlen($flag)) . $replace;
    }
    return $content;
  }

  $body = "Content i want to keep content i want to keep <flag> content to wipe over content to wipe over";
  echo replaceend($body,'here is the new content','<flag>');

?>

I think it was missing a { after the if as it returned an error.

 

?php

  function replaceend($content, $replace, $flag) {
    if (($found_flag = strpos($content,$flag))!==FALSE){
      return substr($body,0,$found_flag+strlen($flag)) . $replace;
    }
    return $content;
  }

  $body = "Content i want to keep content i want to keep <flag> content to wipe over content to wipe over";
  echo replaceend($body,'here is the new content','<flag>');

?> 

 

However when i ran the above code it only outputted 'here is the new content'

 

Any ideas, thanks for all the help

 

Alex

 

 

Thanks a million,

 

I also just suddenly thought i was being really thick i don't need to make it replace after the tag, i can just assign the tag back in with the new content.

 

laffin's code:

 

<?php
$body = "Content i want to keep content i want to keep <flag> content to wipe over content to wipe over";
if(($found_flag=strpos($body,'<flag>'))!==FALSE)
   $body=substr($body,0,$found_flag) . "<flag>This is my new content ";
echo $body;   
?>

 

But i think your function is a better solution than adding the tag back in each time.

 

Thanks so much for all the help, :)

 

Alex

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.