草庐IT

str-replace

全部标签

PHP 使用 str_replace 替换或删除空行的简单方法

$line-out=str_replace('\r','',str_replace('\n','',$line-in));以上对我有用,但是,我在某处看到了一个[\n\r]示例,但我似乎找不到它。我只想去掉所有空白行。以上是在foreach循环中。感谢教导。 最佳答案 你不应该在变量名中使用-;)$line_out=preg_replace('/[\n\r]+/','',$line_in);$line_out=str_replace(array("\n","\r"),'',$line_in);手动输入:http://php.net/

php preg_replace ,我需要替换点后的所有内容(例如 340.38888 > 需要清理 340)

我需要替换点之后的所有内容。我知道这可以用正则表达式来完成,但我还是新手,我不明白正确的语法,所以请帮我解决这个问题。我尝试了下面的代码但是没有用:$x="340.888888";$pattern="/*./"$y=preg_replace($pattern,"",$x);print_r($x);谢谢,迈克尔 最佳答案 我可能是错的,但这听起来像是使用RegEx锤子来解决一个明显非钉子形状的问题。如果你只是想截断一个正float,你可以使用$x=340.888888;$y=floor($x);编辑:正如Techpriester的评论

php - 无法使用 str_replace 删除特殊字符

我有一个关于str_replace的小问题。我有一个带有破折号(-)的字符串,如下所示:Iwanttoremove-thedashhtml输出是Iwanttoremovethe–thedash我想这样做:$new_string=str_replace('-','',$string);我尝试用html_entity_decode解析字符串,用htmlspecialchars解析要删除的字符,但没有任何结果。我做错了什么?-编辑-这是我的脚本的完整代码:$title='SuperMarioGalaxy2-DebutTrailer';//FetchedfromtheDB,inth

php - preg_replace() 模式删除 php 中的括号和内容

我想使用preg_replace()删除括号及其内容,但我无法在模式中使用惰性(非贪婪),因为结束括号是结束字符,括号之间的文本始终是一个随机字符长度,可以包含数字、下划线和连字符。代码-$array=array("Textiwanttokeep(txttoremove)","Randomtxt(somemorerandomtxt)","Keepthis(remove)","Ilikebananas(txt)");$pattern="@pattern@";foreach($arrayas$new_txt){$new_outputs.=preg_replace($pattern,'',$

PHP: preg_replace (x) 发生?

我最近问了一个类似的问题,但没有得到明确的答案,因为我太具体了。这个更广泛。有人知道如何替换正则表达式模式中出现的(x)吗?示例:假设我想替换字符串中第5次出现的正则表达式模式。我该怎么做?这是模式:preg_replace('/{(.*?)\|\:(.*?)}/','替换',$this->source);@anubhava请求示例代码(最后一个功能不起作用):$sample='blahasadaasdas{load|:title}stevejobs{load|:css}windowsapple';$syntax=newsyntax();$syntax->parse($sample);

PHP preg_replace - www 或 http ://

真正坚持看似简单的事情。我有一个聊天框/shoutbox,其中可能输入了任意URL。我想找到每个单独的URL(以空格分隔)并将其包装在标签中。示例:Harryyou'reahttp://google.comwizard!=Harryyou'rea$lhttp://google.com$lwizard!示例:Harryyou'reahttp://www.google.comwizard!=Harryyou'rea$lhttp://www.google.com$lwizard!示例:Harryyou'reawww.google.comwizard!=Harryyou'rea$lwww.go

php - preg_replace 匹配但不替换

在下面的代码中,我尝试获取一些文本并在其周围包裹一个htmlanchor:$d="hello,there,how,are,you";$d=preg_replace("/[a-zA-Z]+/","$1",$d);它正确地识别了每个单词并用替换文本替换了每个单词但是它没有将匹配的单词放入字符串中,它只是将其留空。我在PHP5上。我错过了什么?提前致谢。 最佳答案 您忘记捕获匹配项:$d="hello,there,how,are,you";$d=preg_replace("/([a-zA-Z]+)/","$1",$d);//^^youfo

php - 用 PHP 替换多个占位符?

我有一个发送网站电子邮件的功能(使用phpmailer),我想做的基本上是让php用我提供的内容替换email.tpl文件中的所有占位符。对我来说,问题是我不想重复代码,因此我创建了一个函数(如下)。如果没有php函数,我会在脚本中执行以下操作//emailtemplatefile$email_template="email.tpl";//Getcontactformtemplatefromfile$message=file_get_contents($email_template);//Replaceplaceholdersinemailtemplate$message=str_re

php - 如何用 PHP 替换字符串中的多个 %tags%

替换PHP字符串中的一组短标记的最佳方法是什么,示例:$return="Hello%name%,thankyouforyourinterestinthe%product_name%.%representative_name%willcontactyoushortly!";我会在哪里定义%name%是某个字符串,来自数组或对象,例如:$object->name;$object->product_name;等..我知道我可以在一个字符串上多次运行str_replace,但我想知道是否有更好的方法。谢谢。 最佳答案 如果您知道要替换的占位

php - preg_replace http 为 https

简单地说,我需要检查变量$url中的字符串是否是一个简单的http,如果是,将其替换为https-但我无法让它工作-任何想法:$url="http://www.google.com";//examplehttpurl##$url_replaced=preg_replace('#^http://#','https://',$url);//replacehttpwithhttps##干杯! 最佳答案 为什么不用str_replace?$url="http://www.google.com";//examplehttpurl##$url=