Hi I am creating a generator form to replace
some CSS code I have … it will find all the
class and id as well as dots, and place .class
in front of them …
now it’s only replacing the first match
I have set the limit to -1 … why it’s ignoring it??
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>
Your first two regexes can only match at the start of the string (
^only matches at the start of each line if you use the/mmodifier).But those regexes probably wouldn’t do what you want them to do. Right now you’re looking for a dot or a hash, but only if they are the first character of the string/line (or any comma) and replace them with
.classand themselves. The\1is useless because you don’t have a capturing group in your regex.