I’m trying to create a function in PHP which would go through CSS and JS files, and replacing class names and IDs (but not direct HTML elements or the CSS properties within) in CSS selectors without disrupting the other content. However, of all the dumb luck in the world, I am hopeless when it comes to regular expressions.
I have two arrays, one which has the “original” words, and one that has the words I want to convert it to.
$array1 = array("ul", "text", "border");
$array2 = array("bar", "foo", "far");
$str = magic($str, $array1, $array2);
And this magical function would change the following CSS file
#test-text ul {
text-decoration: none;
}
.ul-border span {
border: 1px solid #fff;
}
#text.niceborder {
border-color: #ccc;
}
into
#test-foo ul {
text-decoration: none;
}
.bar-far span {
border: 1px solid #fff;
}
#foo.nicefar {
border-color: #ccc;
}
I’m hoping the same function could be used to do the same thing for jQuery selectors in JS, but if I get the basic logic behind finding appropriate selectors and applying the arrays only for . and # segments, I think I can make a $() adaptation on my own 😉
Thank you kindly
I don’t know why you need it, but I think this is the function you want:
To explain the regular expression:
Also: I have used anonymous functions. If you are not using PHP 5.3.0+ you have to actually create the two mapping functions, and use:
array_map(mySearchRegexFuncion, $search);instead.