我想用一个Newline字符替换多个Newline字符,用一个空格替换多个空格.
我尝试过但preg_replace("/\n\n+/", "\n", $text);失败了!
我也在$ text上进行格式化.
$text = wordwrap($text, 120, '<br/>', true);
$text = nl2br($text);
Run Code Online (Sandbox Code Playgroud)
$ text是用户为BLOG拍摄的大文本,为了更好的格式化,我使用wordwrap.
PHP中没有多字节'preg'函数,这是否意味着默认的preg_functions都是mb安全的?在php文档中找不到任何提及.
删除特殊字符时遇到问题.我想删除除"()/.% - &"之外的所有特殊字符,因为我将该字符串设置为标题.
我编辑了原始代码(见下图):
preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);
Run Code Online (Sandbox Code Playgroud)
但这并不是要删除特殊字符,例如:"â€","â€",","等等.
原始代码:(这有效但删除了这些字符:"()/.% - &")
preg_replace('/[^a-zA-Z0-9_ -]/s', '', $String);
Run Code Online (Sandbox Code Playgroud) 我有包含这样的字符串:
"Favourite bands: coldplay, guns & roses, etc.,"
Run Code Online (Sandbox Code Playgroud)
如何删除逗号和句点preg_replace?
我需要删除内部链接中的第一个正斜杠,格式如下:
/directory/link.php
Run Code Online (Sandbox Code Playgroud)
我需要:
directory/link.php
Run Code Online (Sandbox Code Playgroud)
我不懂正则表达式(preg_replace?),那些斜杠正在杀了我..
我需要你的帮助stackoverflow!
非常感谢你!
我想要:
Run Code Online (Sandbox Code Playgroud)Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net
成为:
Run Code Online (Sandbox Code Playgroud)Here is link: <a href="http://google.com">http://google.com</a> And <a href="http://example.com">http://example.com</a> inside. And another one at the very end: <a href="http://test.net">http://test.net</a>
看起来像一个简单的任务,但我找不到一个有效的PHP函数.你有什么想法?
function make_links_clickable($text){
// ???
}
$text = 'Here is link: http://google.com
And http://example.com inside.
And another one at the very end: http://test.net';
echo make_links_clickable($text);
Run Code Online (Sandbox Code Playgroud) 我想解析一个文件,我想使用php和正则表达式来剥离:
基本上我想删除任何包含的行
/* text */
Run Code Online (Sandbox Code Playgroud)
或多行注释
/***
some
text
*****/
Run Code Online (Sandbox Code Playgroud)
如果可能,另一个正则表达式来检查该行是否为空(删除空行)
那可能吗?有人可以给我发一个正则那样的正则表达式吗?
非常感谢.
我对正则表达式很新.我需要在开头和结尾清空空格中的搜索字符串.示例:"搜索字符串"结果:"搜索字符串"
我有一个模式,作为一个JavaScript解决方案,但我不能让它使用preg_replace在PHP上工作:
有效的Javascript模式:
/^[\s]*(.*?)[\s]*$/ig
Run Code Online (Sandbox Code Playgroud)
我的例子:
$string = preg_replace( '/^[\s]*(.*?)[\s]*$/si', '', " search string " );
print $string; //returns nothing
Run Code Online (Sandbox Code Playgroud)
在解析它告诉我g不被识别所以我不得不删除它并将ig更改为si.
我使用的preg_replace在PHP中找到并替换字符串中的特定单词,就像这样:
$subject = "Apple apple";
print preg_replace('/\bapple\b/i', 'pear', $subject);
Run Code Online (Sandbox Code Playgroud)
结果'梨梨'.
我希望能够做的是以不区分大小写的方式匹配一个单词,但是当它被替换时尊重它的情况 - 给出结果'Pear pear'.
以下作品,但对我来说似乎有点长篇大论:
$pattern = array('/Apple\b/', '/apple\b/');
$replacement = array('Pear', 'pear');
$subject = "Apple apple";
print preg_replace($pattern, $replacement, $subject);
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
更新:继续下面提出的一个出色的查询,为了完成这项任务,我只想尊重'标题案例' - 所以一个单词的第一个字母是否是一个大写.
我想清理URL中的字符串,这是我基本上需要的.
例如.
This, is the URL!
Run Code Online (Sandbox Code Playgroud)
必须回来
this-is-the-url
Run Code Online (Sandbox Code Playgroud) php ×10
preg-replace ×10
regex ×9
multibyte ×1
preg-match ×1
sanitization ×1
string ×1
whitespace ×1