Escping quotes in PHP regex
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.
2 answers
-
answered 2021-03-05 18:42
Petr Hejda
You need to escape the quote by using a backslash.
In your case
/href=\"tel:12.{20}/
Edit: If you use
htmlspecialchars($x)
, the quote sign"
becomes"
, so you need to update the regex to/href="tel:12.{20}/
-
answered 2021-03-05 18:55
Carlos Condore
if your case is that numbers are not having any especial characters you can use this:
$p = '/\d{10}/'; preg_match_all($p, $x, $matchestel); print_r($matchestel);