2012-07-23 185 views
1

你好我创建一个发电机形式,以取代 一些CSS代码我有...它会发现所有的 class和id以及点,并将其放置的.class 在他们面前。 ..preg_replace函数替换所有匹配

现在它只是取代第一场比赛 我已设置限制为-1 ...为什么它忽略了它?

HTML:

<form action="get_html.php" method="post" id="form"> 
    html:<textarea rows="50" cols="80" id="html_box" name="html" type="text" align="texttop"></textarea> 
    <input id="button" type="submit" value="submit"></input> 
</form> 

get_html.php

<?php 
    $html= $_POST["html"]; 
    $string = (string)$html; 
    $patterns = array(); 
    $patterns[0] = '/^[.]/'; 
    $patterns[1] = '/^[#]/'; 
    $patterns[2] = '/,/'; 
    $replacements = array(); 
    $replacements[0] = '.class .\1'; 
    $replacements[1] = '.class #\1'; 
    $replacements[2] = ', .class \1'; 
    $str= preg_replace($patterns,$replacements, $string, -1); 
?> 
<textarea rows="50" cols="80"><?php echo $str; ?></textarea> 
+0

preg_replace在哪里? – madfriend 2012-07-23 08:06:01

+0

我的不好,编辑对不起 – 2012-07-23 08:09:14

+1

通常是错误的方法 – diEcho 2012-07-23 08:15:03

回答

2

你的前两个正则表达式只能匹配字符串的开始(^仅在每个行的开始,如果你使用的匹配/m修饰符)。

但这些正则表达式可能不会做你想让他们做的事情。现在你正在寻找一个点或一个散列,但只有当它们是字符串/行(或任何逗号)的第一个字符并将它们替换为.class和它们自己时。 \1是无用的,因为你的正则表达式中没有捕获组。

+0

谢谢你是非常有帮助的! – 2012-07-23 08:25:43

+0

thx bro我现在正在工作! :d – 2012-07-23 11:20:04