469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

remove from a string

Is there a function in PHP that will remove a blob from inside a
string. Similar to the substr() function, but instead of giving me the
string I want it to remove the string.

For example, I have the string "john goes running" I want to be able
to tell it to remove 5 characters starting at position 4, which would
get me "john running"

Thanks,

Aug 25 '05 #1
4 18862

werdna.si...@gmail.com wrote:
Is there a function in PHP that will remove a blob from inside a
string. Similar to the substr() function, but instead of giving me the
string I want it to remove the string.

For example, I have the string "john goes running" I want to be able
to tell it to remove 5 characters starting at position 4, which would
get me "john running"


You could combine str_replace and substr:

$str = 'john goes running';
echo str_replace(substr($str,4,5),'',$str);

Ken

Aug 25 '05 #2

we**********@gmail.com wrote:
Is there a function in PHP that will remove a blob from inside a
string. Similar to the substr() function, but instead of giving me the
string I want it to remove the string.

For example, I have the string "john goes running" I want to be able
to tell it to remove 5 characters starting at position 4, which would
get me "john running"


Check out str_replace()
http://us3.php.net/manual/en/function.str-replace.php

Aug 26 '05 #3
But then you end up replacing every instance of the substring should it
appears more than once. Better to just glue the two remaining ends
together:

substr($s, 0, 4) . substr($s, 4 + 5);

Aug 26 '05 #4
Chung Leong wrote:
But then you end up replacing every instance of the substring should it
appears more than once. Better to just glue the two remaining ends
together:

substr($s, 0, 4) . substr($s, 4 + 5);


There's another function that can be used if you want to replace only
one sub string, substr_replace(). See
<http://www.php.net/substr_replace>

Example:

<?
$str = 'john goes running and then goes to the store';
echo 'Original string: ' . $str . '<br>';
echo 'Using str_replace: ' .
str_replace(substr($str,4,5),'',$str).'<br>';
echo 'Using substr_replace: ' . substr_replace($str,'',4,5);
?>

Results:

Original string: john goes running and then goes to the store
Using str_replace: john running and then to the store
Using substr_replace: john running and then goes to the store

Ken

Aug 26 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Christopher Armstrong | last post: by
12 posts views Thread by Sam Collett | last post: by
31 posts views Thread by Extremest | last post: by
15 posts views Thread by morleyc | last post: by
10 posts views Thread by Mike Copeland | last post: by
26 posts views Thread by Brad | last post: by
10 posts views Thread by Tony Johansson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.