Jump to content

I just dont think I understand it


psymastr

Recommended Posts

Hello.  Im trying to teach myself Regex stuff, and Ive come to a roadblock.  I really dont know what is wrong with this code.

 

I want it to parse

<li> TEXT <br />

into

<li> TEXT </li>

but I seem to have the syntax of the regex part wrong.  Whenever I run the code, both my echo statements produce the original $test string.  Can someone explain to me what the problem is?

 

<?php
$test = "<li>Somestuff<br />";
echo $test . "\n";
$test = preg_replace('/(\<li\>)(.+)(\<\/br \/\>)/', "<li>\\2</li>\n", $test);
echo $test;
?>

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/42871-i-just-dont-think-i-understand-it/
Share on other sites

The red part will not match: <\/br \/\>

 

A cleaner regex would be:

%<li>(.+?)< br />% (make sure you remove the space before "br")

 

The % delimiters prevent you from escaping the /, and the ungreedy quantifier (.+?) is required if your string contains more than one li/br pair.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.