如何从字符串

时间:2018-04-16 08:05:06

标签: php regex preg-match

我有这个字符串并尝试获取电话号码:+420 731 58 34 12。 我怎么能用PHP做到这一点?

    <p><strong>Studio Diana</strong><br>Lidická 25<br>Brno, 602 00<br>Tel.: +420 731 58 34 12<br>E-mail: <a href="mailto:diana@studio-diana.cz">diana@studio-diana.cz</a><br><a href="http://www.studio-diana.cz/" target="_blank">http://www.studio-diana.cz</a><br><strong>&nbsp;</strong><br><strong><em>Otevírací doba:</em></strong><br><em>Po-Čt: 8:00–18:00&nbsp;</em><br><em>Pá: 8:00–15:00</em></p>

1 个答案:

答案 0 :(得分:-1)

 $string = '<p><strong>Studio Diana</strong><br>Lidická 25<br>Brno, 602 00<br>Tel.: +420 731 58 34 12<br>E-mail: <a href="mailto:diana@studio-diana.cz">diana@studio-diana.cz</a><br><a href="http://www.studio-diana.cz/" target="_blank">http://www.studio-diana.cz</a><br><strong>&nbsp;</strong><br><strong><em>Otevírací doba:</em></strong><br><em>Po-Čt: 8:00–18:00&nbsp;</em><br><em>Pá: 8:00–15:00</em></p>';


function get_phone_number_from_string($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
$parsed = get_phone_number_from_string($string, '<br>Tel.:', '<br>E-mail:');

echo $parsed; //return  +420 731 58 34 12