Page 1 of 1
Help needed for use of preg_replace
Posted: Sun Jul 12, 2009 7:12 am
by sspadmin
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
Re: Help needed for use of preg_replace
Posted: Sun Jul 12, 2009 10:11 pm
by sspadmin
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
Re: Help needed for use of preg_replace
Posted: Mon Jul 13, 2009 6:21 am
by prometheuzz
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
With the solution already provided, you should be able to figure it out with the following hint:
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
Posted: Mon Jul 13, 2009 7:19 am
by sspadmin
Thanks... It helped a lot
Re: Help needed for use of preg_replace
Posted: Mon Jul 13, 2009 7:21 am
by prometheuzz
sspadmin wrote:Thanks... It helped a lot
You're welcome.
Re: Help needed for use of preg_replace
Posted: Thu Jul 16, 2009 9:14 pm
by sspadmin
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?
Re: Help needed for use of preg_replace
Posted: Fri Jul 17, 2009 3:31 am
by prometheuzz
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?
The backslash is used as an escape characters, so you will need to escape the backslash itself if you're looking for it:
If that was not the problem, you will have to elaborate on the term "not working".
Re: Help needed for use of preg_replace
Posted: Fri Jul 17, 2009 4:14 am
by turbolemon
I don't know how efficient this is, but you could use the following approaches to extract the filename:
Code: Select all
$string = "c:\mydocs\projects\graphics\l\Logo6.jpg";
$arr = explode('\\',$string);
$filename = array_pop($arr);
echo $filename;
You could also use a regex for the same thing:
Code: Select all
$string = "c:\mydocs\projects\graphics\l\Logo6.jpg";
$result = preg_match('/\\([\w\.]+)$/iU',$string,$matches);
$filename = $matches[1];
echo $filename;
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).
Re: Help needed for use of preg_replace
Posted: Fri Jul 17, 2009 4:29 am
by prometheuzz
turbolemon wrote:...
$result = preg_match('/\\([\w\.]+)$/iU',$string,$matches);
...
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.
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
Posted: Fri Jul 17, 2009 7:52 am
by sspadmin
Thanks prometheuzz for providing "point to" solution. I was looking for that kind of solution only which worked perfectly...
Re: Help needed for use of preg_replace
Posted: Fri Jul 17, 2009 8:06 am
by prometheuzz
sspadmin wrote:Thanks prometheuzz for providing "point to" solution. I was looking for that kind of solution only which worked perfectly...
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.
Good luck.