如何识别网页中的电话号码?

时间:2016-08-13 09:56:57

标签: java php web

如何识别网页中的电话号码?

skype点击通话是如何工作的。 我想在网页中构建一个检测电话号码的应用程序。如何通过Skype点击呼叫?

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式查找输入段落中的所有电话号码,然后将其替换为周围的锚点标记,例如<a href="tel: <phonenumber>"><phonenumber></a>

你的代码应该是这样的:

<强>输出

INPUT PARAGRAPH                                                                                                                                                                 
-----------                                                                                                                                                                     
<p>Lets say that this is a sample paragraph with one phone number like 800-965-2321 
and another phone number let's say 70923-23124</p>                                          
-----------                                                                                                                                                                     

OUTPUT PARAGRAPH                                                                                                                                                                
-----------                                                                                                                                                                     
<p>Lets say that this is a sample paragraph with one phone number like <a href="tel: 800-965-2321">800-965-2321</a>    
and another phone number let's say <a href="te: 70923-23124">70923-23124</a></p>                                                                                                                                                       
-----------         

Java代码

import java.util.regex.Matcher;
import java.util.regex.Pattern;

// our main class becomes a file but the main method is still found
public class HelloWorld
{
  public static void main(String[] args)
  {
    String htmlParagraph = "<p>Lets say that this is a sample paragraph with one phone number like 800-965-2321 and another phone number let's say 70923-23124</p>";
    System.out.println("INPUT PARAGRAPH\n-----------\n"+htmlParagraph+"\n-----------\n");
    Pattern p = Pattern.compile("([\\d -]{8,13})");
    Matcher m = p.matcher(htmlParagraph); 
    htmlParagraph = m.replaceAll(" <a href=\"tel:$1\">$1</a> ");
    System.out.println("OUTPUT PARAGRAPH\n-----------\n"+htmlParagraph+"\n-----------\n");
  }
}