在PHP中如何在特定单词后面执行子字符串?

时间:2016-12-20 05:50:21

标签: php wordpress

我有一个这样的字符串:

标题2016年12月15日0评论/主题/来自joe blow Facebook Twitter Google+ LinkedIn我想展示的非常重要的内容。我不关心标题和社交媒体的话。

我想删除字符串以显示“LinkedIn”之后的所有内容。

5 个答案:

答案 0 :(得分:3)

您可以使用explode function provided by php

执行此操作
$str = "December 15, 2016/0 Comments/topic/by joe blow Facebook Twitter Google+ LinkedIn My really important content that I want to display";
$arr = explode("LinkedIn", $str);
echo(trim($arr[1]));

输出

My really important content that I want to display

答案 1 :(得分:1)

您可以使用strstr它将启动您想要的字词或字母的字符串。

$str = "December 15, 2016/0 Comments/topic/by joe blow Facebook Twitter Google+ LinkedIn My really important content that I want to display";

        $str= strstr($str, 'LinkedIn');
        $str = trim($str,'LinkedIn');
        echo $str;

答案 2 :(得分:0)

这是我的代码:

$string = 'Title December 15, 2016/0 Comments/topic/by joe blow Facebook Twitter Google+ LinkedIn My really important content that I want to display';
$result = trim(substr($string, strpos($string, 'LinkedIn') + strlen('LinkedIn'))); 
echo $result;

我希望这有助于您的要求。

substr将返回字符串

的一部分

strpos将在字符串中找到第一次出现子字符串的位置

strlen将获得字符串长度

答案 3 :(得分:0)

试试这个;

$str = "Title December 15, 2016/0 Comments/topic/by joe blow Facebook Twitter Google+ LinkedIn My really important content that I want to display. I don't care about the title and social media words.";
$needle = "LinkedIn";
$pos = strpos ($str, $needle);
$substr = trim(substr($str,$pos + strlen($needle)));

答案 4 :(得分:0)

您可以使用strposstrlensubstr来执行此操作!!

$str = "December 15, 2016/0 Comments/topic/by joe blow Facebook Twitter Google+ LinkedIn My really important content that I want to display";
$index = strpos($str, "LinkedIn"); // find from witch character "LinkedIn" starts
$index += strlen("LinkedIn"); // add "linked in length to $index"
$res = substr($str, $index);  // separate that number of characters from your string
echo $res;