2016-12-20 80 views
1

我有這樣的一個字符串:在PHP中,如何在一個特定的單詞之後做一個子串?

標題2016年12月15日評論/評論/標題/作者joe blow Facebook Twitter Google+ LinkedIn我想展示我真正重要的內容。我不在乎標題和社交媒體的話。

我想剝離字符串以顯示單詞「LinkedIn」後面的所有內容。

+0

您能否引用您的完整字符串? – rahulsm

+0

使用字符串拆分'linkedin' –

回答

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 
+0

也請在陳述後添加分號。 ;) – VishalParkash

+1

得到字符串開頭的空格 – weirdo

+0

whitespaces可以通過修剪()在php –

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; 
+0

將包括字符串'LinkedIn'的一部分 – weirdo

+0

現在它將刪除linkedin :) –

+1

1 voteup..next刪除空格:) – weirdo

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將獲得一個字符串的長度

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))); 
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; 
+0

不錯,但很長。 – VishalParkash

相關問題