PHP: Stripping out predicted text from a string

(Sort of inline with the previous moblog post) All of Sam’s posts are accompanied by a few lines of text that the phone automatically adds. (There’s probably a setting to remove it, but we can’t find it at the moment). So I used the following script to hide the text using php.

<?php
$string = “<$MTEntryBody encode_php=”qq”$>”;
$search = “This message was sent using Picture Messaging!”;
//Change the line above to be the text you always want stripped out
$replace = “”;
//Fill in between the ” ” whatever you want to replace the text with
$pos = strpos($string, $search);
if (is_int($pos)) {
$len = strlen($search);
$string = substr_replace($string, $replace, $pos, $len);
}
echo stripslashes($string);
?>

(Some side references/php functions: substr, substr_replace, strpos (where this code basically comes from - in “user contributed notes”)

Disclaimer: Yes I know this is overkill - and there’s probably a more direct/simpler way. I’m working on that. But I thought this script-snippet with the replace “feature” was pretty interesting, as are some of the other script snippets/functions on that strpos page.

UPDATE: I’ll leave the post here as is just for reference, but there is of course a much simpler solution in the comments!

6 Responses to “PHP: Stripping out predicted text from a string”

  1. Jennifer Says:

    Put that code in place of (and in the SAME place as) your MTEntryBody tag… Keep in mind - it’s only “HIDING” the text - so any page that you have MTEntryBody - you need to add this code. (Also - You’re kind of out of luck on the XML as that won’t process php…)

  2. Matthias Says:

    Hi,

    how about using preg_replace for that job?
    That would read:

    $string = preg_replace(’!’.$search.’!', $replace, $string);

    or probably (for avoiding problems with magic characters in PCRE syntax) the good old str_replace:

    $string = str_replace($search, $replace, $string);

  3. Jennifer Says:

    That is MUCH better! I knew there was a simpler solution! :D

  4. eokyere Says:

    hi, great work you guys have here… i read your blog every now and then, and now that i’m trying to get down and dirty with mt i shd be coming regularly.

    i’m having problems with php includes… they don’t seem to execute. phpinfo shows php is setup fine on the server. is there something i’m missing?

    i’ve tried the forums, but didn’t come out with anything helpful. thanks a lot.

    eokyere

  5. RawBear Says:

    Since I can ass a signature to my emails (from my Palm or Hotmail or Phone) I would prefer to use a code (\end for example) in the signature and have MT drop the rest of the text before posting. That would make the snippet provider-independent.

  6. rawbear Says:

    sorry for the fourth word. The s is just beside the d. My spell checker didn’t pick up, of course!