scriptygoddess

30 Dec, 2003

PHP: Stripping out predicted text from a string

Posted by: Jennifer In: Script snippet

(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

December 30th, 2003 at 10:21 pm

Avatar

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

January 5th, 2004 at 7:21 pm

Avatar

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

January 5th, 2004 at 8:03 pm

Avatar

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

4 | eokyere

January 7th, 2004 at 7:07 am

Avatar

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

February 15th, 2004 at 6:30 pm

Avatar

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

February 15th, 2004 at 6:31 pm

Avatar

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

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements