How can I find only the telephone number (1234827382) in the following string ($x)?
IMPORTANT: I used htmlspecialchars()
function on $x
(text). Without useing this function the regex works. I need to find a regex solution when I use htmlspecialchars()
before regex.
font-size: 14px; color: #333;" >asd:</td> <td width="150" align="left" valign="top"
bgcolor="#f5f5f5" style="padding: 6px; border: 1px solid
#eaeaea;font-family: Arial, Helvetica, sans-serif; font-size: 14px; color:
#333;" >Tel:</td> <td align="left" valign="top" style="padding: 6px;
border: 1px solid #eaeaea;font-family: Arial, Helvetica, sans-serif;
font-size: 14px; color: #333;" ><a href="tel:1234827382">1234827382</a></td> </tr>
<tr> <td width="150" align="left"
I have tried:
$p = '/href="tel:12.{20}/';
preg_match_all($p, $x, $matchestel);
print_r($matchestel);
If I try escape with \ then it also dont work:
$p = '/href=\"tel:12.{20}/';
If the text doesnt contain quote: <a href=tel:1234827382" (instead of this: <a href="tel:1234827382" )
then it finds, and after that I could remove the href" from the beginning. I know it is not the best solution.
I think the problem is that I need to escape the " from pattern.
My Answer:
You can use the following regex pattern to find the telephone number in the string after using htmlspecialchars():
php
$x = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec 1234827382 nisi. Sed euismod, velit ac ultricies.";
preg_match('/\b\d{10}\b/', htmlspecialchars_decode($x), $matches);
$telephone_number = $matches[0];
echo $telephone_number;
This regex pattern `\b\d{10}\b` will match exactly 10 digits in the string, which corresponds to the telephone number in your example. By using `htmlspecialchars_decode()` function before applying the regex, you can decode the HTML entities back to their original characters and then search for the telephone number.
Rate this post
4 of 5 based on 8783 votesComments