Jump to content

preg_match to find a string


Firestarter

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.