Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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_Arrays_Regex - Fatal编程技术网

Php 数组中的转换替换字符串

Php 数组中的转换替换字符串,php,arrays,regex,Php,Arrays,Regex,我有以下代码: <?php $text = 'Hey @Mathew, have u talked with @Jhon today?'; $text = preg_replace('/(^|\s)@([A-Za-z0-9]*)/', '\1<a href="profile.php?user=\\2">@\\2</a>',$text); ?> 那么,如何获得最后的结果呢?在替换文本之前,您可以使用preg\u match查找字符串中的所有用户: 例

我有以下代码:

<?php
$text = 'Hey @Mathew, have u talked with @Jhon today?';
    $text = preg_replace('/(^|\s)@([A-Za-z0-9]*)/', '\1<a href="profile.php?user=\\2">@\\2</a>',$text);
?>

那么,如何获得最后的结果呢?

在替换文本之前,您可以使用
preg\u match
查找字符串中的所有用户:

例如:

$text = 'Hey @Mathew, have u talked with @Jhon today?';

preg_match($pattern, $text, $matches);
var_dump($matches);

$text = preg_replace('/(^|\s)@([A-Za-z0-9]*)/', '\1<a href="profile.php?user=\\2">@\\2</a>',$text);
$text='Hey@Mathew,你今天和@Jhon谈过了吗?';
预匹配($pattern,$text,$matches);
var_dump($matches);
$text=preg_replace('/(^|\s)@([A-Za-z0-9]*)/','\1',$text);

你必须改变你的模式,这样才能工作。

如果你使用
preg\u replace\u回调,你可能会在执行基于正则表达式的搜索和替换时收集匹配项:

$text = 'Hey @Mathew, have u talked with @Jhon today?';
$names = [];
$text = preg_replace_callback('/(^|\s)@([A-Za-z0-9]*)/', function($m) use (&$names) {
        $names[] = $m[2];
        return $m[1] . '<a href="profile.php?user=' . $m[2] . '">@' . $m[2] . '</a>';
    },$text);
echo "$text\n";
print_r($names);
$text='Hey@Mathew,你今天和@Jhon谈过了吗?';
$names=[];
$text=preg_replace_回调('/(^|\s)@([A-Za-z0-9]*)/”,函数($m)使用(&$names){
$names[]=$m[2];
返回$m[1]。';
}美元文本);
回显“$text\n”;
打印(姓名);

输出:

Hey <a href="profile.php?user=Mathew">@Mathew</a>, have u talked with <a href="profile.php?user=Jhon">@Jhon</a> today?
Array
(
    [0] => Mathew
    [1] => Jhon
)
嘿,你今天和我谈过了吗? 排列 ( [0]=>Mathew [1] =>Jhon )

注意:匹配的数组变量通过
use(&$names)
语法传递给匿名函数。
$m
是一个匹配对象,在第一项中包含整个匹配,并在后续项中捕获。

我想您可以使用类似于
/@[a-z0-9]+/sim
的正则表达式,即:

$text = 'Hey @Mathw, have u talked with @Jhon today?';
preg_match_all('/@[a-z0-9]+/sim', $text , $handles_array, PREG_PATTERN_ORDER);
$text = preg_replace('/(@[a-z0-9]+)/sim', '<a href="profile.php?user=$1">$1</a>', $text);
print($text);
print_r($handles_array[0]);
$text='Hey@Mathw,你今天和@Jhon谈过了吗?';
preg_match_all('/@[a-z0-9]+/sim',$text,$handles_数组,preg_模式顺序);
$text=preg_replace(‘/(@[a-z0-9]+)/sim’,‘,$text);
打印(文本);
print_r($handles_array[0]);

输出:

Hey <a href="profile.php?user=@Mathw">@Mathw</a>, have u talked with <a href="profile.php?user=@Jhon">@Jhon</a> today?Array
(
    [0] => @Mathw
    [1] => @Jhon
)
嘿,你今天和我谈过了吗
(
[0]=>@Mathw
[1] =>@Jhon
)

现场演示:


注:

“这是数组的一个示例…”


我不知道有哪种编程语言,数组是用花括号
{}
声明的,对象通常是。你是说括号
[]

PHP给你的是什么
{Mathew',Jhon'}
?看。“这是数组的一个例子…”我不知道有哪种编程语言,数组是用花括号声明的
{}
,对象通常是。你是说括号
[]
?@WiktorStribiżew这正是我需要的。谢谢。这不是答案,但您的模式可以通过编写
@(?代替
(^ |\s)来改进@
注意:未定义变量:模式您需要自己定义模式,因为您的模式会在preg_match返回的matches数组中返回一个空格,所以我没有包含它。请尝试阅读我链接的文档,而不是复制和粘贴代码。似乎OP在替换后需要一个名称数组。
Hey <a href="profile.php?user=@Mathw">@Mathw</a>, have u talked with <a href="profile.php?user=@Jhon">@Jhon</a> today?Array
(
    [0] => @Mathw
    [1] => @Jhon
)