Jump to content

Regex help matching several words in one long text


chrisran

Recommended Posts

Hi !

 

I've got a very long text like this:

 

$var = "Blabla text text text [...] bla bla text text some weird text NEWCU - NEW Customer 0 -> 1 <br /> AMWHA - Some Description 0 -> 2 <br />  INST3 - Install Services 1 <br /> MORES - More Services 0 -> 1 <br /> bla bla bla text text text";

 

In my script I now have these keywords (NEWCU, AMWHA, MORES) set to 0 by default:

 

$newcu = 0;

$amwha = 0;

$mores = 0;

 

Now I'd like to set them to the value after the corresponding -> so for example NEWCU gets 1, AMWHA gets 2 and MORES gets 1.

 

How do I preg_match that ?

 

$newcu = preg_match("/NEWCU - [^] (.?) -&gt (.?)/",$var); <-- does not work.

 

I'd like to grep on "NEWCU -" then skip everything until the -&gt and put the first digit before -&gt (here 0) into $newcu[1] and the digit after -&gt into $newcu[2]..

 

Anyone an idea ? :)

 

Thanks

 

 

Link to comment
Share on other sites

Thanks for your reply mike, but it does not work this way:

 

I wrote a small testprogram to check:

 

 

$string = "NEWCU - Cust No 331351    0 -> 1";

$newcu = preg_match("/NEWCU - [^0-9]*([0-9]+) -> ([0-9]+)/",$string, $matches);
$first = $matches[1];
$second = $matches[2];

echo "<BR><BR>NEWCU: $newcu ] $first -> $second<BR><BR>";

 

 

Result:

NEWCU: 0 ] ->

 

 

Any Idea ?

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.