Firestarter Posted January 13, 2007 Share Posted January 13, 2007 I want to find a string of text then print out the text after it.This is what i'm using now } elseif (preg_match('/CustName\:\s+([^\n]+)/i',$string,$matches)) {It finds a line that begins with CustName: and grabs the text after that ending at the end of the line.But now I have to find a line that begins with </pre><pre><b>role</b>: and do the same thing but with all the special characters in there i'm lost and can't figure it out. And help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/ Share on other sites More sharing options...
c4onastick Posted January 13, 2007 Share Posted January 13, 2007 You're on the right track, a few things:[code]preg_match('/^CustName:\s*(.+)/', $string, $match);[/code]The colon isn't a metacharacter so you don't need to escape it. As long as you don't use the 's' modifier the dot metacharacter can't match a newline (\n), so you can get away with using the dot in a greedy fashion.Post some sample data. But from what it sounds like you're describing you may want something like this:[code]preg_match_all('/^(\w+):\s*(.+)/m', $string, $matches, PREG_SET_ORDER);[/code]This will pull out all the little "label: text text text" pairs. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-159976 Share on other sites More sharing options...
Firestarter Posted January 14, 2007 Author Share Posted January 14, 2007 Here is my code http://sftp.biz/scripts/whois/index.txt you can use it by going to http://sftp.biz/scripts/whois/index.php this script locates the owner of and ip address. It does a pretty good job. Thanks for your help. But the only thing I have trouble is ripe.net one of the sites that it uses to get whois info has way too many variables to allow it to get the right info so I added some extra code. If you have any ideas how I can improve it input is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-160750 Share on other sites More sharing options...
c4onastick Posted January 14, 2007 Share Posted January 14, 2007 Looks good, and works good too! I usually use cURL for doing things of this nature, its more robust. http://www.php.net/manual/en/ref.curl.phpIntegrating curl into your script may give you some additional flexibility down the road. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-160754 Share on other sites More sharing options...
Firestarter Posted January 15, 2007 Author Share Posted January 15, 2007 Thanks it's going pretty good just have to keep testing it. I use it mostly as a plugin for another script located below. But it's great for checking ips in my web site logs and email headers.[img]http://sftp.biz/scripts/counter/image.php?site=test&show=counter[/img] Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-160825 Share on other sites More sharing options...
Firestarter Posted January 17, 2007 Author Share Posted January 17, 2007 Is it possible to use a preg_match but if the match contains a certain word just ignore it. I have some code below but would like it to just continue searching for matches if what it finds contains a word. Thank you[code] preg_match('/org-name:\s*(.+)/',$ripedata,$matches); if ($matches[1] == "")preg_match('/role\<\/b\>:\s*(.+)/',$ripedata,$matches); if ($matches[1] == "")preg_match('/descr:\s*(.+)/',$ripedata,$matches); $isp = $matches[1];[/code] Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-162564 Share on other sites More sharing options...
effigy Posted January 17, 2007 Share Posted January 17, 2007 Can you give an example? Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-162783 Share on other sites More sharing options...
Firestarter Posted January 18, 2007 Author Share Posted January 18, 2007 I would like to use the preg_match('/role\<\/b\>:\s*(.+)/',$ripedata,$matches); command and if it finds a word say hostmaster it would skip it and not return a match. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-163309 Share on other sites More sharing options...
effigy Posted January 18, 2007 Share Posted January 18, 2007 Use a negative lookahead: [tt]%role\</b\>:\s*(?!hostmaster)(.+)%[/tt] Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-163313 Share on other sites More sharing options...
Firestarter Posted January 18, 2007 Author Share Posted January 18, 2007 Thanks for pointing me in te right direction. I used preg_match('%/role\</b\>:\s*(?!hostmaster)(.+)/%',$string,$matches); Worked great. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-163653 Share on other sites More sharing options...
effigy Posted January 18, 2007 Share Posted January 18, 2007 You're not matching forward slashes on the ends are you? I used the percent sign as a delimiter to avoid escaping forward slashes; therefore, you do not need the slashes by the percent signs unless you're really matching them. Delimiters are flexible in PCRE. Link to comment https://forums.phpfreaks.com/topic/34029-preg_match-to-find-a-string/#findComment-163731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.