473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

basic string replacement

If I have a string such as "/ Support us / Volunteering" how do I get
rid of the first slash and not both of them? I had a go at ltrim but
with no success. $path = ltrim($path, "/")
Thanks,

Jul 17 '05 #1
3 1163
Gonzillaaa wrote:
If I have a string such as "/ Support us / Volunteering" how do I get
rid of the first slash and not both of them? I had a go at ltrim but
with no success. $path = ltrim($path, "/")
Thanks,


$path=preg_replace('`/\s*`','',$path,1);

http://www.php.net/preg_replace

HTH

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jul 17 '05 #2
Thanks Justin

I've always found regular expression intimidating.

Jul 17 '05 #3
I was always surprised that PHP didn't have a "limit" argument for
str_replace() that would limit the number of replacements made. If
anyone is interested, I wrote this guy. There's probably a better way,
but it seems to work:

function str_replace_limit($search, $replace, $subject, $limit, &$count
= null) {
$count = 0;

if($limit == 0 || strpos($subject, $search) === false)
return $subject;

$pos = 0;
for($i = 0; $i < $limit; $i++) {
$pos = strpos($subject, $search, $pos);
if($pos === false)
return str_replace($search, $replace, $subject, $count);

$pos += strlen($search);
}

$count = $limit;
$pre = str_replace($search, $replace, substr($subject, 0, $pos));
$post = substr($subject, $pos);

return $pre . $post;
} //str_replace_limit

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Juha Suni | last post by:
Hi! I have managed to live without using too much regular expressions so far, and now that I need one, I need some help too. I have a string containing a (possibly large) block of html. I need...
5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
7
by: RFleming | last post by:
I am not trying to come across as trying to get all my questions answered without doing any work, I am just hoping for someone(s) to suggest the best places and terminology to use when trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.