Jump to content

regular expression issue


hamza

Recommended Posts

I need to get the content of this div.

but getting nothing

$str = "<div class='mainful'>this is testing of the string n thats it </div>";
$str = preg_quote($str, "/");
$result = preg_match_all("/^<div class=\/'mainful\/'>(.*?)<\/div>$/", $str , $matches_results);
if($result){
    print_r($matches_results); 
    exit;
} else {
     echo "no match ";
}

Link to comment
Share on other sites

 

I changed what you used slightly but this will match both your class and the content of the div

 



preg_match_all("|<div class='(.*?)'>(.*?)</div>|",
    "<div class='mainful'>this is testing of the string n thats it </div>",
    $out, PREG_PATTERN_ORDER);
echo "<pre>";
print_r($out);

 

 

I need to get the content of this div.

but getting nothing

$str = "<div class='mainful'>this is testing of the string n thats it </div>";
$str = preg_quote($str, "/");
$result = preg_match_all("/^<div class=\/'mainful\/'>(.*?)<\/div>$/", $str , $matches_results);
if($result){
    print_r($matches_results); 
    exit;
} else {
     echo "no match ";
}

Link to comment
Share on other sites

@Drongo_III

 

There's no need to pass the PREG_PATTERN_ORDER flag, it's the default. Though your pattern would match all DIVs with just a class attribute, so it's not really a practical solution.

 

@hamza

 

I think your problem is using ^ and $ at the start/end of the pattern. Together these require that the pattern is the *only* contents of the string. You can omit either or both to remove that restriction from one or both sides of the pattern.

Link to comment
Share on other sites

Well you could change it so it matches the class as a literal and then it would only target that div.

 

Didn't know pattern_order was the default. I've always used it just for the sake of it. I've only used regular expressions for pretty basic stuff to be honest.

 

Thanks for the tip :)

 

 

@Drongo_III

 

There's no need to pass the PREG_PATTERN_ORDER flag, it's the default. Though your pattern would match all DIVs with just a class attribute, so it's not really a practical solution.

 

@hamza

 

I think your problem is using ^ and $ at the start/end of the pattern. Together these require that the pattern is the *only* contents of the string. You can omit either or both to remove that restriction from one or both sides of the pattern.

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.