user1858780 user1858780 - 2 months ago 13
HTML Question

Convert raw phone numbers into clickable links

I have a directory website which has tons of phone numbers, all in the same format. I'd like to turn them into clickable links especially for mobile.

I simply want to add this format to the numbers:

<a href="tel:555-555-5555">555-555-5555</a>

The issue is I have tons of pages and the numbers are getting pulled in using this code:

$html []= 'Phone: '.$row['phone'].' <br/>';

That results in a layout like this:

Phone: (770)431-9787

Is there anything I can do to that line of code to turn it into a hyperlink? Something like this maybe? I get errors, but is anything like that possible?

$html []= 'Phone: '.$row<a href="tel:['phone']">['phone']</a>.' <br/>';


Use PHP's double-quotes string interpolation:

$html[] = "Phone: <a href='tel:{$row['phone']}'>{$row['phone']}</a><br/>";  

In your example, you have the a tag outside the string, which is unrecognizable by PHP.