Hi I am creating fetch class and I got a problem with regexp in php. For example this is a text: <b>details</b> </a> </td> </tr></table> </div> <p class="desc"> Some text here… <i> Written by Admin </i> </p> Some text again And I want to grab everything between <p class="desc"> and Written by. So result in this case should be: Some text here… <i> How to this with PHP?
Hi, You can use a simple explode() sequence if you like: This code will run as it is: <?php // This is our HTML $html = <<<html <b>details</b> </a> </td> </tr></table> </div> <p class="desc"> Some text here. <i> Written by Admin </i> </p> html; $get = explode('<p class="desc">', $html); $get = explode('Written by', $get[1]); // Remove any whitespace / newlines $get = trim($get[0]); echo $get; ?> PHP: Jay
Use the "s" modifier to make dot metacharacter in the pattern matches all characters, including newlines. Debug preg_match("/<p.*>(.*)Written by/s", $input, $output); print_r($output); PHP: Real preg_match("/<p.*>(.*)Written by/s", $input, $output); echo $output[1]; PHP:
I think you want to change <p.*> to <p.*?> - otherwise the * will be greedy. If there are any tags in the paragraph (e.g., <i>) then it will grab everything up until the last of them.