Php得到特定的字符串

时间:2016-07-21 11:04:14

标签: php string function text

从以下字符串Duitsland中仅提取单词/Duitsland|/groepsreizen fietsen|Stars/1的php函数是什么?

我尝试了所有的东西,但没有找到合适的方法。

4 个答案:

答案 0 :(得分:0)

http://php.net/manual/de/function.str-replace.php

<w:r w:rsidRPr="00E05157">
    <w:rPr>
        <w:rStyle w:val="MyCharacterStyle"/>
    </w:rPr>
    <w:t>17-Jan-14</w:t>
</w:r>

结果:$result= str_replace("Duitsland", "", "/Duitsland|/groepsreizen fietsen|Stars/1");

答案 1 :(得分:0)

有多种方法可以完成这项工作。一种方法是使用正则表达式。在preg_match()中使用正则表达式来查找字符串的特定单词。

$str = "/Duitsland|/groepsreizen fietsen|Stars/1";
preg_match("/[^|\/]+/", $str, $match);
echo $match[0];

您可以在demo

中对其进行测试

答案 2 :(得分:0)

with strpos function find require o/p
$haystack = '/Duitsland|/groepsreizen fietsen|Stars/1';
$needle = 'Duitsland';

if (strpos($haystack,$needle) !== false) {
    echo $needle;
}

答案 3 :(得分:0)

我认为这就是你要找的东西:
(此代码不需要知道之前的单词是什么,它只是在字符串中寻找第一个(尽可能长的)单词)

<?php

$str = "/Duitsland|/groepsreizen fietsen|Stars/1";

preg_match("/\w+/i", $str, $matches);

$first_word = array_shift($matches);

echo $first_word;

无论在该单词之前有多少个非字母符号,它都会起作用,即它不依赖于任何固定数量的其他字符。

演示:http://ideone.com/GX8IT6