【问题标题】:PHP: Change preg_replacePHP:更改 preg_replace
【发布时间】:2016-12-11 07:28:31
【问题描述】:

您好,我尝试从链接中删除“@”,我该怎么做?

提前致谢。

$string = "@username";   
$mention_url = $WebsiteURL. "/". "\\0";
$string = preg_replace("/(?<!\S)@([0-9a-zA-Z_]+)/", "<a href='$mention_url'>\\0</a>", $string);

默认链接是:

http://localhost/@username

我喜欢这个(删除@):

http://localhost/username

【问题讨论】:

  • echo str_replace("@","",$mention_url);
  • Str_replace('/@', '/', $url);。 $url 应该包含 $mention_url 和用户名 ($srting)
  • 我尝试使用 str_replace 不起作用
  • 你还好吗? str_replace 不会修改原始字符串。 $your_string = '一些字符串' $another_string = str_replace('o', 'a', $your_string); $your_string 仍然是“某个字符串”,而 $another_string 是“相同的字符串”

标签: php regex string preg-replace


【解决方案1】:

嗯,你可以使用它,它也可以工作。我在这里测试过--https://regex101.com/r/jqPsfL/1

$da = "http://localhost/@username";
echo preg_replace("/\/\@(\w+)/","/$1",$da);

我认为你的默认数据是

http://localhost/@username

你想删除@。因此,解决方案基于此。如果我不明白你的问题。请你解释一下你想要什么。

希望对你有帮助。

【讨论】:

    【解决方案2】:
    $WebsiteURL = 'http://www.walla.co.il';
    $string = "@username";   
    $string = preg_replace("/@([0-9a-zA-Z_]+)/", "<a href='$WebsiteURL/$1'>$0</a>", $string);
    

    【讨论】:

    • 请扩展您的答案以解释它如何回答问题。代码转储答案不是首选。
    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 2011-01-14
    • 2013-08-03
    相关资源
    最近更新 更多