473,574 Members | 3,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19023

werdna.si...@gm ail.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(sub str($str,4,5),' ',$str);

Ken

Aug 25 '05 #2

we**********@gm ail.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(sub str($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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12701
by: Christopher Armstrong | last post by:
Hello! I'm trying to write a part of a program that will remove all files in its directory. I have tried the std::remove feature of the standard library, but I don't know its syntax. Also, what's the difference between std::remove and std::erase? Thanks for your time!
12
55541
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are also removed) So far I have (val is value, ar is array, returns new array):
4
3956
by: u7djo | last post by:
Hi, I'm currently building an application in Access and as part of this need to import forms and modules from another database. Some of the imports will be revisions of existing forms/modules so I need to check and see they already exist before importing them so I can remove the old one. I've figured out how to remove the classes and modules...
1
19009
by: Craig Buchanan | last post by:
what is the fastest way to remove a value from a string array? something like: dim x as string() = {"A","B","C","D"} 'remove C x.Clear(x, x.IndexOf(x, "C"), 1) Questions:
3
5389
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some example first:
31
4566
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there are more objects that match the first object that i grabbed. If they match then I put them in an array. I would like to remove each match from...
15
50194
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
10
3184
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to remove the punctuation characters from the "first name" substring. I've looked at the basic_string in C++, but I can't find the functions that will...
26
13772
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma and return "12384" What is the most efficient, fastest way to approach this? Thanks,
10
2403
by: Tony Johansson | last post by:
Hello! I have a collection with a lot of object. Each object has the following definition. public class ParametermappingObj { private string Name {get;set;} private string Id {get;set;} some other field defined here ....
0
7842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8110
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3775
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.