Regex to find substring inside an html attribute [duplicate]

I don’t think you need, nor should use a regex for this. It is unclear what you want to do with the found line breaks but this should give you a starting point with parsers.

$string = '<div>
  <input data-content="This is a text string with a <br /> inside of it" />
</div>';
$doc = new DOMDocument();
$doc->loadHTML($string);
$inputs = $doc->getElementsByTagName('input');
foreach($inputs as $input) {
    preg_match_all('/<br\h*\/?>/', $input->getAttribute('data-content'), $linebreaks);
    print_r($linebreaks);
}

Depending out what you want to do preg_match_all may or may not be necessary. The important part of this is that $input->getAttribute('data-content') will give you a string of the data/attribute your want.

Leave a Comment