How search the output only `-` in any DOM using PHP script?

109 views Asked by At

How search the output only - if any DOM as below?

  1. <p>-</p>
  2. <p><span style="font-size: medium;">-</span></p>
  3. etc.

Currently I just use the codes as below to find this output - :

$input = `<p>-</p>`;
if($input == `<p>-</p>`):
   return true;
else:
   return false;
endif;

Any better ideas?

2

There are 2 answers

1
Ulver On BEST ANSWER

The accepted answer would not work for special cases like if the tag attribute values contains >-< or if - is not wrapped within the tags:

$input = '<span title="A valid title >-<">Should NOT match</span>';
$input = '<span>Should match</span>-';

Instead you could use strip_tags(), which is not as efficient as strpos() but would work for all cases:

return (strip_tags($input) === '-');
1
Josua Marcel C On

try

$input = `<p>-</p>`;
$input = `<p><span style="font-size: medium;">-</span></p>`;
$input = `<p><div>-</div>`;
if(strpos($input, '>-<')):
   return true;
else:
   return false;
endif;