我有以下函数来返回脚本的干净路径.
function cleanPath($path) {
$path = (string) $path;
$path = preg_replace(
array(
'#[\n\r\t\0]*#im',
'#/(\.){1,}/#i',
'#(\.){2,}#i',
'#(\.){2,}#i',
'#('.DIRECTORY_SEPARATOR.'){2,}#i'
),
array(
'',
'',
'',
'/'
),
$path
)
;
return rtrim($path,DIRECTORY_SEPARATOR);
}
Run Code Online (Sandbox Code Playgroud)
PHP给出错误:
警告:preg_replace()[function.preg-replace]:编译失败:丢失)在第18行的 C:\ wamp\www\extlogin\app \ni\inc\classes\cfiletree.php中的偏移量7处
关于什么是错的以及如何解决它的任何想法?
谢谢.
在PHP中,在preg_replace()中使用\ 1或$ 1作为$替换有什么区别?
他们都工作,似乎做同样的事情,但我想我在这里缺少一些东西.
我工作的一个项目,我需要从任何地方替换文本的URL domain.com来www.domain.com向http(s)://www.domain.com和电子邮件地址,以适当的HTML <a>标签.我过去使用的是一个很好的解决方案,但它使用了现在已经折旧的eregi_replace功能.最重要的是,用于此类函数的正则表达式不起作用preg_replace.
所以基本上,用户输入一条消息,其中可能包含/可能不包含链接/电子邮件地址,我需要一个正则表达式,preg_replace用于替换带有HTML链接的链接/电子邮件<a href="link">link</a>.
请注意,我还有其他多个preg_replaces.下面是我目前正在进行的其他替换的代码.
$patterns = array('~\[@([^\]]*)\]~','~\[([^\]]*)\]~','~{([^}]*)}~','~_([^_]*)_~','/\s{2}/');
$replacements = array('<b class="reply">@\\1</b>','<b>\\1</b>','<i>\\1</i>','<u>\\1</u>','<br />');
$msg = preg_replace($patterns, $replacements, $msg);
return stripslashes(utf8_encode($msg));
Run Code Online (Sandbox Code Playgroud) 我需要在一些HTML代码的一部分中找到链接,并用两个不同的绝对或基本域替换所有链接,然后是页面上的链接...
我找到了很多想法并尝试了很多不同的解决方案.在这一方面运气不好......请帮助我!谢谢!!
这是我的代码:
<?php
$url = "http://www.oxfordreference.com/views/SEARCH_RESULTS.html?&q=android";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<table class="short_results_summary_table">');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);
echo "{$table}";
$dom = new DOMDocument();
$dom->loadHTML($table);
$dom->strictErrorChecking = FALSE;
// Get all the links
$links = $dom->getElementsByTagName("a");
foreach($links as $link) {
$href = $link->getAttribute("href");
echo "{$href}";
if (strpos("http://oxfordreference.com", $href) == -1) {
if (strpos("/views/", $href) == -1) {
$ref = "http://oxfordreference.com/views/"+$href;
}
else
$ref = "http://oxfordreference.com"+$href;
$link->setAttribute("href", $ref);
echo …Run Code Online (Sandbox Code Playgroud) 我正在尝试为preg_replace找到一个表达式,它删除图像的所有内联css样式.例如,我有这样的文字:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img style="float:left; margin:0 0 10px 10px;" src="image.jpg" /> Proin vestibulum libero id nisl dignissim eu sodales.
Run Code Online (Sandbox Code Playgroud)
我需要让它看起来像:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img src="image.jpg" /> Proin vestibulum libero id nisl dignissim eu sodales.
Run Code Online (Sandbox Code Playgroud)
我已经尝试过几十种表达方式
preg_replace("%<img(.*?)style(.*?)=(.*?)(\'|\")(.+?)(\'|\")(.*?)>%i", "<img\$1\$7>", $article->text)
Run Code Online (Sandbox Code Playgroud)
但没有任何效果.有什么建议?
目前我正在使用
$str = preg_replace("/\([^\)]+\)/", "", $str); // Remove () and Content
$str = preg_replace("/\[[^\)]+\]/", "", $str); // Remove [] and Content
$str = preg_replace("/\{[^\)]+\}/", "", $str); // Remove {} and Content
$str = preg_replace("/[^a-zA-Z0-9]/", "", $str); // Remove all non-alphanumeric characters
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法将这些组合成一个正则表达式语句
我有一个字符串:
$description = '
<a href="http://www.replace.com/1st-link/">1st link in string</a>,
<a href="http://www.ignore.com/">2nd link in string</a>, some other text in string,
<a href="http://www.replace.com/3rd-link/">3rd link in string</a>.
';
Run Code Online (Sandbox Code Playgroud)
我需要使用preg_replace将查询参数"?id = awesome"附加到来自"replace.com"的$ description字符串中的任何URL(忽略所有其他链接,即"ignore.com").
在此先感谢您的帮助!
我试图更好地理解正则表达式,但似乎无法理解这一点.
preg_replace("/[^a-zA-Z0-9.?!\s]/", "", $mystring);
Run Code Online (Sandbox Code Playgroud)
我认为这不会取代短划线,因为在上面的例子中它将被视为"通过".但每次我运行它时,短划线都会被删除.
我需要在php字符串中第一次出现数字后删除所有内容但保留数字.我很难保持这个数字:当我写这个时
preg_replace('/[0-9].*/', '', $string);
Run Code Online (Sandbox Code Playgroud)
它会删除所有内容,包括号码.我的另一个问题是字符串是希伯来语我试图使用这个函数:
$count = mb_strlen($string,'UTF-8');
$i = 0;
while( $i < $count ) {
if( ctype_digit($string[$i]) ) {
echo "First digit found at position $i.";
return;
}
$i++;
}
Run Code Online (Sandbox Code Playgroud)
但它也行不通.任何帮助?
此代码将任何网址转换为可点击的链接:
$str = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '<a href="$1">$1</a>', $str);
Run Code Online (Sandbox Code Playgroud)
如何在url以[character?像这样:
php ×10
preg-replace ×10
regex ×5
url ×2
coding-style ×1
converter ×1
href ×1
image ×1
parameters ×1
preg-match ×1
replace ×1