使用' www'获取所有字词或网址在给定的文本上

时间:2017-09-13 22:53:14

标签: php regex

我需要使用' www'来获取所有的字词或网址。在给定的文本上。这是我到目前为止所尝试的。

<?php 
$needle = 'www';
$sentence = 'This www.google.com is the http://www.facebook.com website that https://www.amazon.com make me awesome';

echo $needle . "<br />";
echo $sentence . "<br /><br />";

if(preg_match('/\b(' . preg_quote($needle, '/') . '\w+)/', $sentence, $match)){
    echo "<pre>";
        print_r($match);
    echo "</pre>";
}
?>

我期望的输出是一个值数组:

www.google.com
http://www.facebook.com 
https://www.amazon.com

但是目前,我的代码不起作用并输出一个空白数组。请帮我解决这个问题。感谢。

3 个答案:

答案 0 :(得分:2)

我认为你在这里不需要正则表达式,最好避免使用它们,所以看看这是否适合你。

由于你只需要一段文本,如果它有针(在这种情况下,www),那么你可以用空格分割字符串,然后通过检查是否一个过滤数组一块包含针。如果是,请将其添加到另一个数组:

<?php 
$needle = 'www';
$sentence = 'This www.google.com is the http://www.facebook.com website that https://www.amazon.com make me awesome';
$pieces = explode(" ", $sentence);
$urls = [];
$urls = array_filter($pieces, function($v) use($needle) {
    return stripos($v, $needle) !== false;
});
var_dump($urls);

/* result */
array(3) {
    [1]=>
    string(14) "www.google.com"
    [4]=>
    string(23) "http://www.facebook.com"
    [7]=>
    string(22) "https://www.amazon.com"
}

Demo

答案 1 :(得分:1)

试试这个我相信你会得到你想要的结果

11:04:58.643 3208-3208/com.****.****E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.****.****, PID: 3208 android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:195)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
    at android.content.res.Resources.getLayout(Resources.java:1142)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:416)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
    at org.chromium.content.browser.input.SelectPopupAdapter.getView(SelectPopupAdapter.java:53)
    at android.widget.AbsListView.obtainView(AbsListView.java:2372)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
    at android.widget.ListView.onMeasure(ListView.java:1315)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
    at android.view.View.measure(View.java:21998)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1471)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

<强>输出

$sentence = 'This www.google.com is the http://www.facebook.com website that https://www.amazon.com make me awesome';
$pattern = '@((https?://)?([-\\w]+\\.[-\\w\\.]+)+\\w(:\\d+)?(/([-\\w/_\\.]*(\\?\\S+)?)?)*)@';

preg_match_all($pattern, $sentence, $matchFound);

echo '<pre>';
print_r($matchFound[0]);

答案 2 :(得分:1)

使用模式\S*www\S*Demo

  

\ S 匹配任何非空白字符(等于[^ \ r \ n \ n \ t \ f])

     

* 量词 - 在零和无限次之间匹配,尽可能多次,根据需要回馈

$needle = 'www';
$sentence = 'This www.google.com is the http://www.facebook.com website that https://www.amazon.com make me awesome';

echo $needle . "<br />";
echo $sentence . "<br /><br />";

if(preg_match('/\S*('.preg_quote($needle, '/').'\S*)/', $sentence, $match)){
    echo "<pre>";
        print_r($match);
    echo "</pre>";
}