preg-replace 相关问题

preg_replace()是一个PHP函数,它使用正则表达式执行字符串替换。

使用正则表达式解析 BBCode 文本时出现“编译失败:括号不匹配”错误

在尝试使用正则表达式模式数组解析 BBCode 时,出现错误“编译失败:偏移量 11 处的括号不匹配”。 我不知道偏移量 11 是什么意思,我已经...

回答 3 投票 0

在数组中的每个字符串之前添加美元符号?

我有这个字符串: $str = "(he+is+genius*2)/clever";在数组中看起来像这样; 大批 ( [0] => ( [1] => 他 [2] => + [3] => 是 [4] => + [5] => 天才 [6...

回答 2 投票 0

str Replace / preg 使用数组将希伯来语单词替换为希伯来语单词

好的。所以我有一个数组,其中包含我需要在字符串中查找并替换的单词 与他们的键(数组中该单词的键)。 在英语中,这有效。 函数replace_twophrase_words($string) { $

回答 2 投票 0

PHP 在字符串中查找 URL 并创建链接。如果还没有链接

我想在字符串中查找链接尚未存在于链接中的 URL 我当前的代码: $text =“http://www.google.com 是一个很棒的网站。访问 http://goog...

回答 2 投票 0

为什么 PHP 函数 preg_replace('/a*?/', '!', 'a') 给出 '!!!'但 '/a*/' 给出 '!!'

注意到 PHP 函数: preg_replace('/a*?/', '!', 'a') 给出“!!!”但: preg_replace('/a*/', '!', 'a') 给出“!!”。 我想我明白为什么'!!',因为 in 匹配空子字符串,但是......

回答 1 投票 0

如何使用 preg_replace() 函数替换多个不同的字符?

如何替换@和.使用 PHP 中的 preg_replace() 函数从电子邮件地址中提取带有 - 符号的符号?

回答 3 投票 0

脏话过滤器,用星号替换脏话的每个字母

我有一个工作函数,它获取一系列坏词,然后用星号替换坏词。 当我升级到PHP7后,自从使用了preg_replac后,我不得不使用preg_replace_callback...

回答 2 投票 0

带有 preg_replace_callback 的正则表达式用于脏话过滤器

我有一个工作函数,它获取一系列坏词,然后用星号替换坏词。 当我升级到 PHP7 时,我必须使用 preg_replace_callback,因为 preg_replace e mod...

回答 2 投票 0

将带有查询字符串的 URL 转换为“搜索引擎友好”URL

我想知道是否可以重写为htaccess中的重写而设计的URL。 例如 我有网址:http://example.com/page.php?page=5&action=something 但我想改变...

回答 1 投票 0

删除字符串中的换行符和回车符

我正在尝试计算文本区域中的字符数,但它也计算“ “ 和 ” " 作为字符;我如何删除它们以便只计算实际的文本输入? $pure_chars = preg_replace("...

回答 2 投票 0

替换 preg_replace 中的字符串

<?php $a="php.net s earch for in the all php.net sites this mirror only function list online documentation bug database Site News Archive All Changelogs just pear.php.net just pecl.php.net just talks.php.net general mailing list developer mailing list documentation mailing list What is PHP? PHP is a widely-used..."; ?> 我想突出显示特定单词。 例如php、net和func: php.net s earch for in the all **php**.**net** sites this mirror only **func**tion list online documentation bug database Site News Archive All Changelogs just pear.**php**.**net** just pecl.**php**.**net** just talks.php.net general mailing list developer mailing list documentation mailing list What is **PHP**? **PHP** is a widely-used... 您可以执行以下操作: // your string. $str = "..............."; // list of keywords that need to be highlighted. $keywords = array('php','net','fun'); // iterate through the list. foreach($keywords as $keyword) { // replace keyword with **keyword** $str = preg_replace("/($keyword)/i","**$1**",$str); } 即使关键字是任何其他更大字符串的子字符串,上述内容也会替换关键字。要仅将关键字替换为完整单词,您可以执行以下操作: $str = preg_replace("/\b($keyword)\b/i","**$1**",$str); $words = 'php|net|func'; echo preg_replace("/($words)/i", '**$1**', $a);

回答 2 投票 0

如何去除字符串中的斜杠及其之间的数字内容

考虑以下字符串: /0123456789/abcdefg 我需要删除两个斜杠之间的所有数字以及删除斜杠。我知道 preg_replace,但从未使用过

回答 3 投票 0

preg_replace 使用模式作为替换数据数组的索引

我想知道是否有一种简单的方法可以使用 preg_replace 中的匹配模式作为替换值数组的索引。 例如 preg_replace("/\{[a-z_]*\}/i", "{$data_array[ ]}", $s...

回答 4 投票 0

使用 PHP preg_replace 从 HTML 标签中删除所有属性,仅保留 class 和 id 属性

所以,到目前为止我只能保留一个属性,但我试图将 class 和 id 属性保留在 HTML 标签中 代码: $string = '一些 所以,到目前为止我只能保留一个属性,但我试图将 class 和 id 属性保留在 HTML 标签中 代码: $string = '<div id="one-id" class="someClassName">Some text <a href="#" title="Words" id="linkId" class="classLink">link</a> with only the class and id attrtibutes./div>'; preg_replace("/<([a-z][a-z0-9]*)(?:[^>]*(\sclass=['\"][^'\"]*['\"]))?[^>]*?(\/?)>/i", '<$1$2$3>', $string); 输出: <div class="someClassName">Some text <a class="classLink">link</a> with only the class and id attrtibutes./div> 我试图从每个标签中删除除 class 和 id 属性之外的所有其他属性。 迭代 dom 中的所有节点,然后反向循环所有属性,以便您可以安全地修剪不在白名单中的属性。 (我修复了示例输入中 </div> 中的拼写错误。) 代码:(演示) $html = '<div id="one-id" class="someClassName">Some text <a href="#" title="Words" id="linkId" class="classLink">link</a> with only the class and id attrtibutes.</div>'; $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); foreach ($xpath->query('//*') as $node) { for ($i = $node->attributes->length - 1; $i >= 0; --$i) { $attr = $node->attributes->item($i); if (!in_array($attr->name, ['id', 'class'])) { $node->removeAttribute($attr->name); } } } echo $dom->saveHTML(); 输出: 有些文本链接仅包含类和 ID 属性。

回答 1 投票 0

仅替换字符串中的整个单词匹配

我想使用 php 替换完整的单词 例子 : 如果我有 $text = "你好你好你好,你好"; 我用 $newtext = str_replace("你好",'NEW',$text); 新文本应该看起来...

回答 4 投票 0

文本中数字后紧跟字母时插入分隔空格

我需要分隔数字后紧跟单词的子字符串。 $input = '新的 stackoverflow 244code 7490script 设计'; $output = "新的 stackoverflow 244 代码 7490 脚本设计&

回答 5 投票 0

PHP:在字符串中用空格分隔字母数字单词

如何在一个语句中用空格分隔字母数字值 例子 : $arr="新的 stackoverflow 244code 7490 脚本设计"; 那么如何用空格分隔字母和数字,例如: ...

回答 5 投票 0

使用正则表达式替换字符串中的最后一个逗号

我有一个像这样的字符串: “第 1 项、第 2 项、第 3 项”。 我需要的是将其转换为: “第 1 项、第 2 项和第 3 项”。 实际上,将最后一个逗号替换为“and”。谁能帮我解决这个问题吗?

回答 3 投票 0

一系列 unicode 点的正则表达式 PHP

我正在尝试从字符串中删除所有字符,除了: 字母数字字符 美元符号 ($) 下划线 (_) 代码点 U+0080 和 U+FFFF 之间的 Unicode 字符 前三张我都有了

回答 3 投票 0

在这种情况下是 preg_replace 还是 mb_ereg_replace?

我有这个正则表达式用于匹配 Unicode 中的空格: /^[\pZ\pC]+|[\pZ\pC]+$/u 我什至不确定它的作用,但它似乎有效。现在,在这种情况下,哪个函数更适用,为什么? $s...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.