Jump to content

[SOLVED] preg match pattern


dtdetu

Recommended Posts

hello i get content of a page with curl i and i am trying to find if there is a "Your message has been sent to" in the page i use this code but it doesnt work. can u check this.

 

if (preg_match("[Your message has been sent to\!]", $store)) 
{ 
	...
} 
else 
{ 
	...
}

Link to comment
Share on other sites

You need to account for the situation where something would be found before and after the string you are looking for.

 

<?php
$string = "hallo this
an attempt at
Matching This String which should return true";

if (preg_match("/^.*Matching This String.*/s", $string)) {
echo 'Match Found!';
} else {
echo 'No Match Found!';
}

?>

 

The "s" modifier at the end simply makes the dot include newlines. The star after the dot (.) both allows for nothing to exist, and everything. I.e the characters allowed before and after the pattern to be matched, may be repeated 0 or more times.

 

So in your case the pattern would be something like:

<?php
if (preg_match("/^.*Your message has been sent to
.*/s", $string)) {
echo 'Match Found!';
} else {
echo 'No Match Found!';
}

?>

Link to comment
Share on other sites

Actually this would be easier:

 

<?php
$string = "hallo this
an attempt at
Matching This String which should return true";


if (preg_match("/Matching This String/", $string)) {
echo 'Match Found!';
} else {
echo 'No Match Found!';
}

?> 

 

Removing the start "^" indicator will allow you to do this.

Link to comment
Share on other sites

Or you could just use strpos() as I said above (in this case I've used stripos()).

 

$string = "hallo this an attempt at Matching This String which should return true";

if(stripos($string, "matching this string") !== false){
echo "MATHCED";
}else{
echo "NOT MATCHED";
}

Link to comment
Share on other sites

thanks i used stripos now it works good now, i need one more:) i didnt want to open new topic , my question is how can i get the Limoncafe from this with pregmatch all

 

<span style='color:#CC6600;'>Your message has been sent to Limoncafe</span>

 

preg_match_all('~<span style="color:#CC6600;">([^<]*)</span>~',$store,$matches);
echo $matches[1];

 

this code gives me Array only .

 

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.