Help needed for use of preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sspadmin
Forum Newbie
Posts: 5
Joined: Sun Jul 12, 2009 7:09 am

Help needed for use of preg_replace

Post 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
Last edited by sspadmin on Sun Jul 12, 2009 10:11 pm, edited 1 time in total.
sspadmin
Forum Newbie
Posts: 5
Joined: Sun Jul 12, 2009 7:09 am

Re: Help needed for use of preg_replace

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Help needed for use of preg_replace

Post 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
sspadmin
Forum Newbie
Posts: 5
Joined: Sun Jul 12, 2009 7:09 am

Re: Help needed for use of preg_replace

Post by sspadmin »

Thanks... It helped a lot
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Help needed for use of preg_replace

Post by prometheuzz »

sspadmin wrote:Thanks... It helped a lot
You're welcome.
sspadmin
Forum Newbie
Posts: 5
Joined: Sun Jul 12, 2009 7:09 am

Re: Help needed for use of preg_replace

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Help needed for use of preg_replace

Post 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:

Code: Select all

$find = '\\';
If that was not the problem, you will have to elaborate on the term "not working".
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: Help needed for use of preg_replace

Post 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).
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Help needed for use of preg_replace

Post 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];
sspadmin
Forum Newbie
Posts: 5
Joined: Sun Jul 12, 2009 7:09 am

Re: Help needed for use of preg_replace

Post by sspadmin »

Thanks prometheuzz for providing "point to" solution. I was looking for that kind of solution only which worked perfectly...
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Help needed for use of preg_replace

Post 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.
Post Reply