Help needed for use of preg_replace
Moderator: General Moderators
Help needed for use of preg_replace
How to use php's preg_replace to start trim operation from
begining of string and and end at a perticular word?
For example I want to use PHP preg_repalce to display "is
Paris" from a sentance "The capital of France is Paris".
(I know that i have to trim upto "France" from begining of
sentance ).
Thanks in advance
begining of string and and end at a perticular word?
For example I want to use PHP preg_repalce to display "is
Paris" from a sentance "The capital of France is Paris".
(I know that i have to trim upto "France" from begining of
sentance ).
Thanks in advance
Last edited by sspadmin on Sun Jul 12, 2009 10:11 pm, edited 1 time in total.
Re: Help needed for use of preg_replace
Thanks McInfo , for you help and pointing out typos!
One more help needed.If string is like "'The capital of France is Paris.Among UK,France and USA, I like France most.".How can I trim upto the last value of 'France' and display the word "most" only.
Thanks in advance
One more help needed.If string is like "'The capital of France is Paris.Among UK,France and USA, I like France most.".How can I trim upto the last value of 'France' and display the word "most" only.
Thanks in advance
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Help needed for use of preg_replace
With the solution already provided, you should be able to figure it out with the following hint:sspadmin wrote:Thanks McInfo , for you help and pointing out typos!
One more help needed.If string is like "'The capital of France is Paris.Among UK,France and USA, I like France most.".How can I trim upto the last value of 'France' and display the word "most" only.
Thanks in advance
strpos — Find position of first occurrence of a string
strrchr — Find the last occurrence of a character in a string
Re: Help needed for use of preg_replace
Thanks... It helped a lot
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Help needed for use of preg_replace
You're welcome.sspadmin wrote:Thanks... It helped a lot
Re: Help needed for use of preg_replace
How to find out " \ " character?
I want to find out last character of Back Slash ("\") in a string "c:\mydocs\projects\graphics\l\Logo6.jpg" . I want to extract file name "Logo6.jpg".
I used strrchr , ltrim and Substr , but it seems it is not working for this character. Any solution?
I want to find out last character of Back Slash ("\") in a string "c:\mydocs\projects\graphics\l\Logo6.jpg" . I want to extract file name "Logo6.jpg".
I used strrchr , ltrim and Substr , but it seems it is not working for this character. Any solution?
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Help needed for use of preg_replace
The backslash is used as an escape characters, so you will need to escape the backslash itself if you're looking for it:sspadmin wrote:How to find out " \ " character?
I want to find out last character of Back Slash ("\") in a string "c:\mydocs\projects\graphics\l\Logo6.jpg" . I want to extract file name "Logo6.jpg".
I used strrchr , ltrim and Substr , but it seems it is not working for this character. Any solution?
Code: Select all
$find = '\\';- turbolemon
- Forum Commoner
- Posts: 70
- Joined: Tue Jul 14, 2009 6:45 am
- Location: Preston, UK
Re: Help needed for use of preg_replace
I don't know how efficient this is, but you could use the following approaches to extract the filename:
You could also use a regex for the same thing:
If you are looking to include the backslash in the result, you would have to re-add it to the former example as it is lost in the explode. In the latter you would just change $filename = $matches[1] to $filename = $matches[0] (the full match as opposed to the sub-pattern).
Code: Select all
$string = "c:\mydocs\projects\graphics\l\Logo6.jpg";
$arr = explode('\\',$string);
$filename = array_pop($arr);
echo $filename;
Code: Select all
$string = "c:\mydocs\projects\graphics\l\Logo6.jpg";
$result = preg_match('/\\([\w\.]+)$/iU',$string,$matches);
$filename = $matches[1];
echo $filename;
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Help needed for use of preg_replace
To match a backslash, you will need to use four (!) backslashes since the backslash has a special meaning inside a string literal AND inside the regex language as well.turbolemon wrote:...
$result = preg_match('/\\([\w\.]+)$/iU',$string,$matches);
...
But your suggestion will fail as soon as a file name with characters outside the class [\w.] comes along. Note that you need not escape the DOT inside a character class. Inside a character class, it does not have any special meaning.
But, an (IMO) easier (and better) way of using preg_match for this would be to do:
Code: Select all
preg_match('/[^\\\\]+$/i', "c:\\mydocs\\projects\\graphics\\l\\Logo6.jpg", $match);
echo $match[0];Re: Help needed for use of preg_replace
Thanks prometheuzz for providing "point to" solution. I was looking for that kind of solution only which worked perfectly...
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Help needed for use of preg_replace
No problem. Just be sure you understand what you copy-and-paste. Also, when doing this a large number of times, or on extremely large strings, you might be better of using "normal" string functions as McInfo suggested.sspadmin wrote:Thanks prometheuzz for providing "point to" solution. I was looking for that kind of solution only which worked perfectly...
Good luck.