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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>');

?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

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.