0

Any one can help me in finding any tag present in text as html format.

Ex: $text="<a href>the hero</a>";

find "<a" is present in $text or not in php.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71

3 Answers3

1

If you simply want to check for the presence of "<a" in a string use strpos. It is much faster than preg_* which must first compile a regular expression.

<?php
$exists = (strpos($text , '<a') !== false);
?>
AlexanderZ
  • 521
  • 2
  • 6
0

For parsing HTML is best to use some kind of DOM parser, more info here: PHP HTML DOM Parser, but if your goal is only to check existence of html tag, preg_match can be the solution:

$text="<a href>the hero</a>";
var_dump(preg_match("/\<a.*\>(.*?)\<\/a\>/si", $text));
Community
  • 1
  • 1
Peter Krejci
  • 3,182
  • 6
  • 31
  • 49
-2

Maybe:

if(str_replace("<a", "<a", $text)) {
    echo "ok";
} else {
    echo "not ok";
}

See the manual for str_replace.

Ry-
  • 218,210
  • 55
  • 464
  • 476
clement
  • 4,204
  • 10
  • 65
  • 133
  • You are doing unnecessarily job using `str_replace` - replacing string with the same string. Using such construction decreases code readability. – Peter Krejci Jan 11 '12 at 21:54