我试图使用正则表达式从标签中提取信息,然后根据标签的各个部分返回结果.
preg_replace('/<(example )?(example2)+ />/', analyze(array($0, $1, $2)), $src);
所以我抓住零件并将其传递给analyze()函数.在那里,我想基于部件本身做工作:
function analyze($matches) {
if ($matches[0] == '<example example2 />')
return 'something_awesome';
else if ($matches[1] == 'example')
return 'ftw';
}
Run Code Online (Sandbox Code Playgroud)
但是一旦我进入analyze函数,$matches[0]就等于字符串' $0'.相反,我需要$matches[0]引用preg_replace()调用的反向引用.我怎样才能做到这一点?
谢谢.
编辑:我刚看到preg_replace_callback()函数.也许这就是我要找的......
我有一些看起来像这样的HTML:
<h2>
Fund Management</h2>
<p>
The majority of property investments are now made via our Funds.</p>
Run Code Online (Sandbox Code Playgroud)
尝试使用正则表达式去除h2标记,但由于开始和结束h2标记之间的空间不起作用.
preg_replace('/<h2>(.+?)<\/h2>/', '', $content);
Run Code Online (Sandbox Code Playgroud)
关于如何使这项工作的任何想法?
另外我理想情况下会更换h1-h6标签,所以它可能需要[1-6]或其他东西?
是否可以使用javascript复制此内容?
preg_replace('/(.gif|.jpg|.png)/', '_thumb$1', $f['logo']);
Run Code Online (Sandbox Code Playgroud)
编辑 - 我没有得到这个代码的跟随错误,
未终止的字符串文字
$('#feed').prepend('<div class="feed-item"><img src="'+html.logo.replace(/(.gif|.jpg|.png)/g, "_thumb$1")+'"/>
<div class="content">'+html.content+'</div></div>').fadeIn('slow');
我试图// comments用我的php preg_replace()在我的javascript中取消注释并创建一个preg_replace,它应该执行以下操作:
1.当评论从新行开始时,删除整行:
// COMMENTS .....
2.当评论落后于脚本时,在1 TAB之后//删除该评论部分
exampleScript(); // (1space) comments
3.在http://中与//不匹配
这个pregreplace做了上述工作,但是,它目前删除了3行代码//.(请参阅下面的错误匹配标题)它应该跳过.
$buffer = preg_replace('/(?<!http:)\/\/\s*[^\r\n]*/', '', $buffer);
Run Code Online (Sandbox Code Playgroud)
很好的比赛
//something
// something *!&~@#^hjksdhaf
功能();// comment
假匹配
(/\/\.\//)
"//"
"://"
Run Code Online (Sandbox Code Playgroud)
那么,我如何过滤这三个错误匹配以及如何更改以下正则表达式?
(?<!http:)\/\/\s*[^\r\n]*
Run Code Online (Sandbox Code Playgroud)
PS,我不希望使用其他人的代码minifiers /框架与他们自己的开销.就我自己而言.
我究竟做错了什么?
<?php
$imageurl = $pagename1;
$imageurl = preg_replace('/.asp/', .$g_sites_img2.'.jpg', $pagename1);
?>
Run Code Online (Sandbox Code Playgroud)
我试图逃避.的preg_replace.
我也尝试过:
<?php
$imageurl = $pagename1;
$imageurl = preg_replace('/\.asp/', .$g_sites_img2.'\.jpg', $pagename1);
?>
Run Code Online (Sandbox Code Playgroud)
为什么它仍然给我一个错误?
我有一个MySQL数据库,从PHP驱动的表单提供数据.表列整理为utf8_bin,连接字符集设置为UTF-8,因为是HTML.
经过广泛的谷歌搜索后,我似乎找不到任何明确的方法来使用preg_replace来删除不需要的字符(和数字),但保留大写/小写的重音符号,变音符号和空格.我拼凑起来的东西,似乎工作 - 但我不理解的事,所以不知道它是多么安全.因此,使用escape子句加倍:
$lname = preg_replace("/(<\/?)(\w+)([^>]*>)/e","", $lname);
$lname = mysql_real_escape_string($lname);
Run Code Online (Sandbox Code Playgroud)
我真正需要的是那种可以采用以下名称的条款(我的,作为一个例子):"ÉamonnMacLochlainn"并存储它,而不是"c389616d6f6e6eMacLochlainn"我也看了strip_tags,允许"ÁÉÍÓÚáéíóú ".这是前进的方向吗?
任何帮助 - 特别是对此片段中发生的事情的解释(\ w +位) - 将不胜感激.
我需要替换curl拍摄的页面中的网址,并向图像和链接添加正确的链接。我的PHP curl代码是:
<?php
$result = '<a href="http://host.org"><img src="./sec.png"></a>
<link href="./styles.css" rel="alternate stylesheet" type="text/css" />
<script type="text/javascript" src="./style.js"></script>';
echo $result;
if (!preg_match('/src="https?:\/\/"/', $result)) {
$result = preg_replace('/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', "src=\"http://google.com/\\3\"", $result);
}
echo $result;
if (!preg_match('/href="https?:\/\/"/', $result)) {
$result = preg_replace('/href="(http:\/\/([^\/]+)\/)?([^"]+)"/', "href=\"http://google.com/\\3\"", $result);
}
echo $result;
?>
Run Code Online (Sandbox Code Playgroud)
输出为:
//original links
<a href="http://host.org"><img src="./sec.png"></a>
<link href="./styles.css" type="text/css" />
<script src="./style.js"></script><br />
//fixed SRC path
<a href="http://host.org"><img src="http://google.com/./sec.png"></a>
<link href="./styles.css" type="text/css" />
<script src="http://google.com/./style.js"></script>
//fixed HREF path
<a href="http://google.com//google.com/./sec.png"></a>
<link href="http://google.com/./styles.css" type="text/css" />
<script src="http://google.com/./style.js"></script> …Run Code Online (Sandbox Code Playgroud) 我在preg_replace有问题.
我有一个文本文件,其中包括:
#define FWTE_BLACK "{000000FF
#define FWTE_TEST "{006600FF
...
Run Code Online (Sandbox Code Playgroud)
完整文本如下:https://eval.in/133942
我想将FF每行的结尾替换为}"
喜欢这样:
#define FWTE_BLACK "{000000}"
#define FWTE_TEST "{006600}"
...
Run Code Online (Sandbox Code Playgroud) 我正在尝试更改字符串中文本的颜色:
我的字符串:
$message="I have a [red][car]";
Run Code Online (Sandbox Code Playgroud)
我想捕获第一个[...]和第二个[...]内的值,然后使用它
<b style='color:color'>car</b>
Run Code Online (Sandbox Code Playgroud)
根据第一个[...]的值更改文本颜色
到目前为止,我有:
echo preg_replace("/\[([^\]]+)\]\[([^\]+])\]/i","<b style='color:$1'>$2</b>",$message);
Run Code Online (Sandbox Code Playgroud)
但它不起作用,orignal字符串作为输出返回.我不知道我的正则表达式失败了.
请帮忙!
php ×10
preg-replace ×10
regex ×6
javascript ×2
diacritics ×1
minify ×1
mysql ×1
obfuscation ×1
preg-match ×1