Re: [PHP] regular expressions help please
On Tuesday 30 April 2002 15:51, Ed Lazor wrote: > I've been banging my head against regular expressions all night... help > would be greatly appreciated. Could you give me examples on how to do the > following? Is this for a programming assignment/exercise? Do you /have/ to use regex? Other methods may be easier. I'll do the two simple ones: > Pull everything except a specific word from a sentence. For example, > pulling everything except the word run from "the water run was steep". a) Use str_replace(), search for 'run ' (or ' run') and replace with ''. b) explode() the string, put 'run' (and any other words you don't want) into an array, then use array_diff() > Pull all words from a string starting with a specific letter or > pattern. For example, pulling all of the words starting with the letter r > from "run beep burp ran rin ron runt zoot zip pow" a) explode() the string, loop through array check 1st char of each element. > I apologize in advance if these are really simple. It's just that I'm new > to regular expressions and reading all of the web page tutorials, manual > pages, and mailing list archive messages has left me thinking I'm > complicating something somewhere, because it really shouldn't be this hard. As you can see, a bit of lateral thinking can get you the same results without the headbanging. Using regex can be expensive in terms of CPU cycles and very often simpler methods will suffice. By all means learn regexes as they are damn useful. But start simple then build upon it. -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* A full belly makes a dull brain. -- Ben Franklin [and the local candy machine man. Ed] */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] regular expressions help please
> -Original Message- > From: John Fishworld [mailto:[EMAIL PROTECTED]] > Sent: 30 April 2002 09:32 > > I'm trying to find files in my array > for example > ="lg_imode.gif" > and > ="/db/imodeklein/edgar-IMODE-1-.gif" > > I want to differentiate between the files with slash at the > front and ones > without so that > I can add a server path ! If all you want is to differentiate the ones where the first letter is a "/", why don't you just check exactly that? foreach ($array as $key=>$fname): if ($fname{0} =='/'): // begins with slash -- add server path endif; endforeach; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
On Tuesday 30 April 2002 16:31, John Fishworld wrote: > I'm trying to find files in my array > for example > ="lg_imode.gif" > and > ="/db/imodeklein/edgar-IMODE-1-.gif" Perhaps you should clarify your problem. First of all does your array contain just gif files (ie *.gif) or does it contain all sorts of files? (ie readme.txt, important.doc, funny.gif etc) -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* In a consumer society there are inevitably two kinds of slaves: the prisoners of addiction and the prisoners of envy. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
Okay right I'm experimenting with an i-mode parser ! I copy the file (url entered) to a local location ! Then read through the whole file line at a time and change/replace the things that need replaceing ! On of the things that I need to replace is the links to the pictures so that they still show even ! I've already got from the original file the original location http://www.whatever.com and now want to insert that in front of the gif files ie $imode_code[$i] = eregi_replace("( On Tuesday 30 April 2002 16:31, John Fishworld wrote: > > I'm trying to find files in my array > > for example > > ="lg_imode.gif" > > and > > ="/db/imodeklein/edgar-IMODE-1-.gif" > > Perhaps you should clarify your problem. First of all does your array contain > just gif files (ie *.gif) or does it contain all sorts of files? (ie > readme.txt, important.doc, funny.gif etc) > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.com.hk > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > > /* > In a consumer society there are inevitably two kinds of slaves: > the prisoners of addiction and the prisoners of envy. > */ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
On Tuesday 30 April 2002 19:17, John Fishworld wrote: > Okay right I'm experimenting with an i-mode parser ! > I copy the file (url entered) to a local location ! > Then read through the whole file line at a time and change/replace the > things that need replaceing ! > On of the things that I need to replace is the links to the pictures so > that they still show even ! > I've already got from the original file the original location > http://www.whatever.com > and now want to insert that in front of the gif files > ie > $imode_code[$i] = eregi_replace("( "\\1$next_path\\2",$imode_code[$i]); > > There are just gif files in this ! > > Does that make more sense ? Unfortunately, no. Could you post say 20 lines of this file you're talking about. -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* You are in a maze of little twisting passages, all different. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
$imode_code = file("$url_file"); $file_name = basename($url_file); $path = dirname($url_file); $stripped_path = eregi_replace("^(.{2,6}://)?[^/]*/", "", $path); $next_path = eregi_replace($stripped_path, "", $path); $next_path_1 = eregi_replace("/$" , "", $next_path); // create and open a file to write to $new_file = "files/".$file_name; $fh = fopen($new_file , "w"); $lines = count($imode_code); // echo "Lines = $lines"; for ($i; $i<$lines; $i++) { // add server path to gifs with front slashes /hello/john.gif // $imode_code[$i] = eregi_replace("([^\"]+[/+]+[a-z_/-]+[gif]+[/$])", "$next_path_1\\0",$imode_code[$i]); // add server path to gifs withour front slashes john.gif // $imode_code[$i] = eregi_replace("\.gif" , "$next_path/\\0",$imode_code[$i]); // changes links so also for server paths for html + chtml + php // $imode_code[$i] = eregi_replace("([a-z_]+[.]+[chtml])", "$next_path_1\\0",$imode_code[$i]); // $imode_code[$i] = eregi_replace("([^\"]+[a-z_]+[\.html]+[\"$])", "$next_path_1\\0",$imode_code[$i]); // $imode_code[$i] = eregi_replace("([^\"]+[a-z_]+[\.php]+[\"$])", "$next_path_1\\0",$imode_code[$i]); // replace imode pictos with a real gif $imode_code[$i] = ereg_replace("([0-9]{5});" , "" , $imode_code[$i] ); // write the whole lot to file fputs ($fh, $imode_code[$i]); } // close file fclose($fh); // display file echo " "; ?> > On Tuesday 30 April 2002 19:17, John Fishworld wrote: > > Okay right I'm experimenting with an i-mode parser ! > > I copy the file (url entered) to a local location ! > > Then read through the whole file line at a time and change/replace the > > things that need replaceing ! > > On of the things that I need to replace is the links to the pictures so > > that they still show even ! > > I've already got from the original file the original location > > http://www.whatever.com > > and now want to insert that in front of the gif files > > ie > > $imode_code[$i] = eregi_replace("( > "\\1$next_path\\2",$imode_code[$i]); > > > > There are just gif files in this ! > > > > Does that make more sense ? > > Unfortunately, no. Could you post say 20 lines of this file you're talking > about. > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.com.hk > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > > /* > You are in a maze of little twisting passages, all different. > */ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
On Tuesday 30 April 2002 19:43, John Fishworld wrote: > $imode_code = file("$url_file"); > $file_name = basename($url_file); > $path = dirname($url_file); > $stripped_path = eregi_replace("^(.{2,6}://)?[^/]*/", "", $path); > > On Tuesday 30 April 2002 19:17, John Fishworld wrote: > > > Okay right I'm experimenting with an i-mode parser ! > > > I copy the file (url entered) to a local location ! > > > Then read through the whole file line at a time and change/replace the > > > things that need replaceing ! > > > On of the things that I need to replace is the links to the pictures so > > > that they still show even ! > > > I've already got from the original file the original location > > > http://www.whatever.com > > > and now want to insert that in front of the gif files > > > ie > > > $imode_code[$i] = eregi_replace("( > > "\\1$next_path\\2",$imode_code[$i]); > > > > > > There are just gif files in this ! > > > > > > Does that make more sense ? > > > > Unfortunately, no. Could you post say 20 lines of this file you're > > talking about. I mean the file that you're readingparsing, not your program. -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* The absent ones are always at fault. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
Duh ! lol sorry ! Example 1 Locations Welcome to Your Preference: 驪 Schwul Lesbisch Example 2 i-mode Auswahl Mehr Karten Karten suchen Impressum Edgars Auswahl verschicken verschicken verschicken verschicken http://www.baesurl.com/imenu/";>i-menu > On Tuesday 30 April 2002 19:43, John Fishworld wrote: > > > $imode_code = file("$url_file"); > > $file_name = basename($url_file); > > $path = dirname($url_file); > > $stripped_path = eregi_replace("^(.{2,6}://)?[^/]*/", "", $path); > > > > On Tuesday 30 April 2002 19:17, John Fishworld wrote: > > > > Okay right I'm experimenting with an i-mode parser ! > > > > I copy the file (url entered) to a local location ! > > > > Then read through the whole file line at a time and change/replace the > > > > things that need replaceing ! > > > > On of the things that I need to replace is the links to the pictures so > > > > that they still show even ! > > > > I've already got from the original file the original location > > > > http://www.whatever.com > > > > and now want to insert that in front of the gif files > > > > ie > > > > $imode_code[$i] = eregi_replace("( > > > "\\1$next_path\\2",$imode_code[$i]); > > > > > > > > There are just gif files in this ! > > > > > > > > Does that make more sense ? > > > > > > Unfortunately, no. Could you post say 20 lines of this file you're > > > talking about. > > I mean the file that you're readingparsing, not your program. > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.com.hk > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > > /* > The absent ones are always at fault. > */ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
On Tue, 30 Apr 2002, Ed Lazor wrote: > Pull everything except a specific word from a sentence. For example, > pulling everything except the word run from "the water run was steep". $str = 'the water run was steep'; print preg_replace('/(\s*water)/', '', $str); > Pull all words from a string starting with a specific letter or > pattern. For example, pulling all of the words starting with the letter r > from "run beep burp ran rin ron runt zoot zip pow" $str = 'run beep burp ran rin ron runt zoot zip pow'; if (preg_match_all('/(\br\w+\b)/', $str, $matches)) print join(' ', $matches[1]); > Pulling all words from a string excluding ones starting with a specific > letter or pattern. For example, pulling all of the words except ones > starting with the letter r from "run beep burp ran rin ron runt zoot zip > pow" $str = 'run beep burp ran rin ron runt zoot zip pow'; if (preg_match_all('/([\b\W][^r]\w+\b)/', $str, $matches)) print join(' ', $matches[1]); > Pulling a word between two other words without having to know what the > word is. For example, pulling whatever word displays between "the" and > "sky". If the string was "the blue sky", the result would be the word > blue. If the string were "the green sky", the result would be the word > green. $str = 'the green sky'; if (preg_match('/the\s+(\S+?)\s+sky/', $str, $matches)) print $matches[1]; > I apologize in advance if these are really simple. It's just that I'm > new to regular expressions and reading all of the web page tutorials, > manual pages, and mailing list archive messages has left me thinking I'm > complicating something somewhere, because it really shouldn't be this > hard. Well, it's not trivial. Regular expressions is a whole complete language, entirely separate from PHP, with a lot to learn. Practice enough, though, and you'll start to see how you can do amazing things with it. Also, I'd recommend using the Perl-style regex (preg_ rather than ereg_ functions) and reading 'man perlre' if it's installed on your system (someone can probably suggest a web location for that text). miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
On Tuesday 30 April 2002 21:09, John Fishworld wrote: > Duh ! lol sorry ! > href="imode.fpl?op=imodecard&prefix=IMODE&nummer=1&suffx=&uid=55%2eFAGAE&pa > > > > Unfortunately, no. Could you post say 20 lines of this file you're > > > > talking about. > > > > I mean the file that you're readingparsing, not your program. Try this: preg_match_all("/src=(\'[a-z0-9_\/-]+\.gif\')/i", $input, $MATCH); print_r($MATCH); -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* Money is its own reward. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
Thanks after playing about with that I've got the following which does seem to work ! $imode_code[$i] = eregi_replace("(src=)(\")([a-z0-9_\/-]+\.gif)(\")", "\\1\\2$path/\\3\\2", $imode_code[$i]); Very very very slowly getting the hang of regexs ! What does your /i do at the end ??? Thanks > Try this: > > preg_match_all("/src=(\'[a-z0-9_\/-]+\.gif\')/i", $input, $MATCH); > print_r($MATCH); > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.com.hk > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > > /* > Money is its own reward. > */ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
On Wed, 1 May 2002, John Fishworld wrote: > Thanks after playing about with that I've got the following which does seem > to work ! > > $imode_code[$i] = eregi_replace("(src=)(\")([a-z0-9_\/-]+\.gif)(\")", > "\\1\\2$path/\\3\\2", $imode_code[$i]); > > Very very very slowly getting the hang of regexs ! > > What does your /i do at the end ??? Makes it case-insensitive, so "gif" and "GIF" are treated identically. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
aha ! thats very strange then because mine works at the moment but if I add the /i at the end then it doesn't ! > On Wed, 1 May 2002, John Fishworld wrote: > > Thanks after playing about with that I've got the following which does seem > > to work ! > > > > $imode_code[$i] = eregi_replace("(src=)(\")([a-z0-9_\/-]+\.gif)(\")", > > "\\1\\2$path/\\3\\2", $imode_code[$i]); > > > > Very very very slowly getting the hang of regexs ! > > > > What does your /i do at the end ??? > > Makes it case-insensitive, so "gif" and "GIF" are treated identically. > > miguel > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expressions help please
I wasn't paying that much attention. The /i is a preg thing. It's the same as changing from ereg to eregi. miguel On Wed, 1 May 2002, John Fishworld wrote: > aha ! > thats very strange then because mine works at the moment but if I add the /i > at the end then it doesn't ! > > > On Wed, 1 May 2002, John Fishworld wrote: > > > Thanks after playing about with that I've got the following which does > seem > > > to work ! > > > > > > $imode_code[$i] = eregi_replace("(src=)(\")([a-z0-9_\/-]+\.gif)(\")", > > > "\\1\\2$path/\\3\\2", $imode_code[$i]); > > > > > > Very very very slowly getting the hang of regexs ! > > > > > > What does your /i do at the end ??? > > > > Makes it case-insensitive, so "gif" and "GIF" are treated identically. > > > > miguel > > > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php