1

regex noob hear. Via PHP and preg_replace()...

I'm trying to make this.

id="New York State"

Into this.

id="New_York_State"

But not this.

id="New_York_State"_class="United_States"


All my attempts have been abysmal!
I've read several tutorials but I am still having a lot of trouble formulating useful patterns with correct syntax.

Please, please, please, include a explanation of the regex pattern with your answer! Also, if you feel up to it, any links to good PHP regex tutorials or articles would be much appreciated. Thanks!

17
  • Show us your attempts. Maybe you're on the good way. Commented May 11, 2013 at 21:00
  • Are you doing the replacement in a full HTML document ? Commented May 11, 2013 at 21:00
  • I think the best way to do this is using DOM, check for all classes and id's and do a space replacement. Commented May 11, 2013 at 21:01
  • 2
    Use simple_load_xml? Commented May 11, 2013 at 21:04
  • 1
    @Terry just curious: you know that class="United States" has a different meaning than class="United_States"? Commented May 11, 2013 at 21:33

3 Answers 3

4

Use SimpleXMLElement

header('Content-Type:text/plain');
$xml = simplexml_load_file('log.svg', "SimpleXMLElement");
recursiveReplace($xml);
echo $xml->asXML();

Function Used

function recursiveReplace(&$xml) {
    foreach($xml as $k => $value) {
        foreach($xml->attributes() as $n => $d) {
            $xml[$n] = preg_replace('/\s+/', '_', $d);
        }
        recursiveReplace($value);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Was waiting for the canonical answer :D
@Baba This seems like a very elegant and approachable solution. I'm working on a WAMP environment, it seems that I need to install a few libraries to be able to use the SimpleXMLElement extension. Not a problem but I was really just looking for a regex pattern. Still super usefull, thank you. Really appreciate it!
3

An extremely bad solution:

$string = 'blabla id="New_York_State" class="United_States" blabla';

$new_string = preg_replace_callback('#\"([^"]*)\"#', function($m){
    return('"'. str_replace(' ', '_', $m[1]) .'"');
}, $string);
echo $new_string;

Output:

blabla id="New_York_State" class="United_States" blabla

Allowing only id= and class= behind the double quotes:

$string = 'blabla id="New_York_State" class="United_States" blabla "this won\'t get underscored" yaaaay';

$new_string = preg_replace_callback('#(?<=id=|class=)\"([^"]*)\"#', function($m){
    return('"'. str_replace(' ', '_', $m[1]) .'"');
}, $string);
echo $new_string;

Output:

blabla id="New_York_State" class="United_States" blabla "this won't get underscored" yaaaay

7 Comments

To tell you the truth I wouldn't know how to do this through the DOM with PHP. Currently I have been loading the <svg> into a browser making applicable changes with JavaScript and copying the <svg> via firebug to a text editor O_o (eye twitch)
I'm NOT a programmer. I'm a graphic design student teaching himself to code. I understand most of the regexp pattern, except... ?<=, and meaning of * in ([^"]*).
@Terry I'm a nobody and I learnt from here and there, but don't worry I'll explain :) (?<=) is a lookbehind assertion for example (?<=a)b this will match b if there is a before it: ab the b here gets matched acb the b here doesn't get matched. * is basically 0 or more times, [] means a character class, so [^"] will match anything but " we add * to it to match several characters if possible [^"]*, so for example this will match abc in abc".
You are not a nobody. Don't say that. Thank you for your help, it was a great explanation,
@Terry haha you're welcome, also you may try regex101 which is a good site to test your regex and also get a bit of explanation on it and you may take a look at this tutorial, have fun !
|
0

There is a function called str_replace.

Your code will be like this:

$id = str_replace(" ", "_", $id); or better $id = str_replace("New York State", "New_York_State", $id);

Source: http://php.net/manual/en/function.str-replace.php

3 Comments

Actually this information is true, but it doesn't answer this particular question.
This will replace all spaces, not just those within id attributes.
If you use it with the DOM, why not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.