如何从字符串中删除单引号,只留下单词(撇号)? [PHP的]

时间:2018-01-11 15:24:09

标签: php

我想从字符串中删除单引号,但不删除作为撇号的引号。

例如:

  

请点击“此处”选择您要登录的帐户

我需要它像

  

请点击此处选择您要登录的帐户

我试过这个

$text = preg_replace( '/[^\da-z ! ?.]/i', ' ', $text );

但它删除了所有单引号。任何帮助赞赏。长时间搜索后,在堆栈溢出中找不到任何解决方案。

1 个答案:

答案 0 :(得分:3)

模式:/\B'|'\B/

Pattern Demo

代码:(Demo

$text="Please click 'here' to choose the account you'd like to 'Login'";
$text = preg_replace("/\B'|'\B/",'', $text);
echo $text;

输出:

Please click here to choose the account you'd like to Login

要处理多个所有格名词(或我们称之为的任何名词),例如girls' dresses,这是一个更强大的调用:

代码:(Demo)(Pattern Demo

$text="Please click 'here' to choose the users' accounts that you'd like to 'use'";
$text = preg_replace("/\B'([a-z-]+)'\B/i",'$1', $text);
echo $text;

输出:

Please click here to choose the users' accounts that you'd like to use