Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何选择多个文本,在一行文本中以@作为文本的第一个字母?_Php_Explode - Fatal编程技术网

Php 如何选择多个文本,在一行文本中以@作为文本的第一个字母?

Php 如何选择多个文本,在一行文本中以@作为文本的第一个字母?,php,explode,Php,Explode,好的,标题问题可能听起来很困惑,是的,我也很困惑。总之,我想要的是: 假设我有这行文字 The quick brown @fox jumps @over the @lazy dog. 这行文本是从数据库动态获取的“单行”,而不是文本数组。假设第一个字母为“@”的文本是指向页面或其他内容的链接,我希望我可以指定放置锚定标记的位置,在我的情况下,我希望在每个以“@”开头的文本上放置锚定标记 我试过爆炸,但似乎爆炸不是这个问题的答案。有人能帮我吗?谢谢。您不想使用它,但要使用正则表达式。要匹配多个事

好的,标题问题可能听起来很困惑,是的,我也很困惑。总之,我想要的是: 假设我有这行文字

The quick brown @fox jumps @over the @lazy dog.
这行文本是从数据库动态获取的“单行”,而不是文本数组。假设第一个字母为“@”的文本是指向页面或其他内容的链接,我希望我可以指定放置锚定标记的位置,在我的情况下,我希望在每个以“@”开头的文本上放置锚定标记

我试过爆炸,但似乎爆炸不是这个问题的答案。有人能帮我吗?谢谢。

您不想使用它,但要使用正则表达式。要匹配多个事件,这就是交易

preg_match_all('/@\w+/', $input, $matches);

        #        @   is the literal "@" character
        #    and \w+ matches consecutive letters

您肯定希望使用将它们转换为链接。或者最好将一些逻辑移到处理函数中。

您可以使用explode来处理带有@before。。。这取决于你想做什么:

//Store the string in a variable
$textVar = "The quick brown @fox jumps @over the @lazy dog.";

//Use explode to separate words
$words = explode(" ", $textVar);

//Check all the variables in the array, if the first character is a @
//keep it, else, unset it
foreach($words as $key=>$val) {
    if(substr($val, 0, 1) != "@") {
        unset($words[$key]);
    } else {
        $words[$key] = "<a href='#'>".$words[$key]."</a>";
    }
}

//You can now printout the array and you will get only the words that start with @
foreach($words as $word) {
    echo $word."<br>";
}
//将字符串存储在变量中
$textVar=“棕色的狐狸跳过了懒狗。”;
//使用explode分隔单词
$words=explode(“,$textVar”);
//检查数组中的所有变量,如果第一个字符是@
//保留它,否则,取消它
foreach($key=>$val的单词){
if(substr($val,0,1)!=“@”){
未设置($words[$key]);
}否则{
$words[$key]=“”;
}
}
//现在,您可以打印出数组,您将只获得以开头的单词@
foreach($words作为$word){
回显$word.“
”; }
您还可以保留不带@的字符串,并使用内爆将所有内容组合在一起:

//Store the string in a variable
$textVar = "The quick brown @fox jumps @over the @lazy dog.";

//Use explode to separate words
$words = explode(" ", $textVar);

//Check all the variables in the array, if the first character is a @
//keep it, else, unset it
foreach($words as $key=>$val) {
    if(substr($val, 0, 1) != "@") {
        //Do nothing
    } else {
        $words[$key] = "<a href='#'>".$words[$key]."</a>";
    }
}

//You can now printout the string
$words = implode($words, " ");
echo $words;
//将字符串存储在变量中
$textVar=“棕色的狐狸跳过了懒狗。”;
//使用explode分隔单词
$words=explode(“,$textVar”);
//检查数组中的所有变量,如果第一个字符是@
//保留它,否则,取消它
foreach($key=>$val的单词){
if(substr($val,0,1)!=“@”){
//无所事事
}否则{
$words[$key]=“”;
}
}
//现在可以打印出字符串了
$words=内爆($words,”);
呼应$words;

一旦你用PHP处理了这一行,你希望它看起来像什么,你想要的最终结果是什么?可能是@mario的重复,谢谢你让我知道,但我的问题有点不同。不过还是要谢谢你。100%同意正则表达式的方法。打败我!哇,这对我来说是新的。但是,我现在的处境很糟糕,我不知怎么地理解了你答案的流程,但似乎我发现很难实现。我刚才在php.net/manual上搜索了preg_match_all,但是您能详细说明我应该做什么吗*现在感觉很傻是的,我终于明白了。谢谢各位。现在我要用preg_replace来麻烦自己了。@CHiRiLo:链接的副本包含类似的方法。另见