[PHP] regular expressions help please
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? Pull everything except a specific word from a sentence. For example, pulling everything except the word run from the water run was steep. 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 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 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. 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. Thanks in advance for any help you can offer. -Ed -- 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 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
[PHP] regular expressions help please
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 ! but as usual I' m having problems with the correct regex At the moment I've got this to find files with forward slash eregi_replace(([^\]+[/+]+[a-z_/-]+[gif]+[/$]) , and it seems to work.(although I think it's wrong) And I'm trying this to find the files without the slash at the front eregi_replace(([^\]+(a-z_)+[gif]$) , which is definately not working. help ! -- 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((img src=).+\.gif\ , \\1$next_path\\2,$imode_code[$i]); There are just gif files in this ! Does that make more sense ? 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((img src=).+\.gif\ , \\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}); , img src=\pictos/\\1.gif\ border=\0\ , $imode_code[$i] ); // write the whole lot to file fputs ($fh, $imode_code[$i]); } // close file fclose($fh); // display file echo IFRAME border=0 marginWidth=0 src=\files/$file_name\ frameBorder=0 width=150 height=131 marginheigth=0 /IFRAME; ? /body /html 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((img src=).+\.gif\ , \\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((img src=).+\.gif\ , \\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 html head meta http-equiv=content-type content=text/html;charset=iso-8859-1 meta name=generator content=Edit Plus titleLocations/title /head body bgcolor=#6699FF div align=center pfont color=blackWelcome tobr /fontfont color=redimg src=lg_imode.gif alt= height=36 width=120 align=baseline border=0br div align=leftYour Preference:/divbr a accesskey=1 href=schwul.php4#63879; Schwul/abr a accesskey=2 href=lesbisch.chtmlLesbisch/abr /fontp/p /div /body /html Example 2 htmlheadtitlei-mode/title/head body div align=centerimg SRC=/imode/img/logo_40.gif vspace=3 alt=logobrimg src=/imode/img/desc.gif alt=E-Cards verschicken/div hr noshade size=2 nbsp;#59106;nbsp;a accesskey=1 href=#sAuswahl/abr nbsp;#59107;nbsp;a accesskey=2 href=imode.fpl?op=categorylistuid=55%2eFAGAEpartner=Mehr Karten/abr nbsp;#59108;nbsp;a accesskey=3 href=imode.fpl?op=searchlistuid=55%2eFAGAEpartner=Karten suchen/abr nbsp;#59109;nbsp;a accesskey=4 href=imode.fpl?op=impressumuid=55%2eFAGAEpartner=Impressum/abr hr noshade size=2 nbsp;a name=sfont color=#ccEdgars Auswahl/font/abr div align=center img src=/db/imodeklein/edgar-IMODE-1-.gif vspace=2br#59091;nbsp;a href=imode.fpl?op=imodecardprefix=IMODEnummer=1suffx=uid=55%2eFAGAEpar tner=verschicken/abr img src=/db/imodeklein/edgar-IMODE-6-.gif vspace=2br#59091;nbsp;a href=imode.fpl?op=imodecardprefix=IMODEnummer=6suffx=uid=55%2eFAGAEpar tner=verschicken/abr img src=/db/imodeklein/edgar-IMODE-2-.gif vspace=2br#59091;nbsp;a href=imode.fpl?op=imodecardprefix=IMODEnummer=2suffx=uid=55%2eFAGAEpar tner=verschicken/abr img src=/db/imodeklein/edgar-IMODE-7-.gif vspace=2br#59091;nbsp;a href=imode.fpl?op=imodecardprefix=IMODEnummer=7suffx=uid=55%2eFAGAEpar tner=verschicken/abr /div hr noshade size=2 nbsp;#59115;nbsp;a accesskey=0 href=http://www.baesurl.com/imenu/;i-menu/a /body /html 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((img src=).+\.gif\ , \\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 ! img src=/db/imodeklein/edgar-IMODE-1-.gif vspace=2br#59091;nbsp;a href=imode.fpl?op=imodecardprefix=IMODEnummer=1suffx=uid=55%2eFAGAEpa 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