7

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?

Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
Nere
  • 4,097
  • 5
  • 31
  • 71
  • 1
    Since it's an exact match, your try is already sufficient. If the match is based on pattern, then you'll need RegEx. – Raptor Jun 08 '15 at 02:06
  • Can you show me the details? – Nere Jun 08 '15 at 02:08
  • The condition is only if `-` existed...its means that no other characters involved. – Nere Jun 08 '15 at 02:09
  • 1
    Oops, apart from using RegEx, you can use `strpos($dom, '-')` – Raptor Jun 08 '15 at 02:15
  • 1
    It should be noted that `p` element can't have a `div` child. – Ram Jun 08 '15 at 02:45
  • 1
    @Imran While making suggested edits to change [tag:bootstrap] to [tag:twitter-bootstrap] please understand that you need to try to fix all the issues with the post not just changing `bootstrap` to `twitter-bootstrap` everywhere and know that bootstrap is not owned by twitter anymore so while changing the tag is valid there is no need to change bootstrap to twitter bootstrap in the text. – Ram Jun 28 '15 at 00:06
  • Thank you for you information. – Nere Jun 28 '15 at 00:22

2 Answers2

2

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;
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
2

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) === '-');
Ulver
  • 905
  • 8
  • 13