Closed Thread Icon

Topic awaiting preservation: Splitting Caps in PHP with Regex Pages that link to <a href="https://ozoneasylum.com/backlink?for=27403" title="Pages that link to Topic awaiting preservation: Splitting Caps in PHP with Regex" rel="nofollow" >Topic awaiting preservation: Splitting Caps in PHP with Regex\

 
Author Thread
divinechaos
Obsessive-Compulsive (I) Inmate

From:
Insane since: Dec 2001

posted posted 01-28-2006 09:03

It's an odd question, but I have an XML file I'm parsing where each node name follows the convention of "PuttingNoSpacesInFrontOfCaps". So, is there a way using ereg or preg to find each match of "[a-z][A-Z]" and split them with a space? I've tried preg_replace(), but it replaces the characters as well, leaving me with "Puttin pace ront aps" (not a viable solution).

I presume I don't have to create an enormous array to hold the translated values ("LastName" => "Last Name"), so does anybody have a solution or another method they use to expand such strings?

Any help appreciated.

Cheers,
DC

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 01-28-2006 11:06

What you need to use, as far as I can see, is php->preg_split instead.

You'll need to change your regex search string so search for one upper-case character and then an unspecified number of lower-case characters. I think (and I'm a little rusty on my regex) you could try "[A-Z]{1}[a-z]*".


Justice 4 Pat Richard

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 01-28-2006 11:17

Back references

In JavaScript you'd simply do : alert( youString.replace( /([^A-Z]*)([A-Z])/g, "$1 $2" ) );
In PHP the replacement string should be ( I hqven't tested ) something like : echo preg_replace( /([^A-Z]*)([A-Z])/g, "$1 $2", $yourString );

[edit2] it's pitq to szitch from UK to FR keyboqrd qll the ti,e [/edit2]


(Edited by poi on 01-28-2006 11:20)

(Edited by poi on 01-28-2006 11:22)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 01-28-2006 11:54

If I remember php->preg_split correctly,

$arrSeperated = preg_split("~[A-Z]~", "TheWordToSplit")
leads to
$arrSeperated ("The", "Word", "To", "Split");
which you then could implode(" ", $arrSeperated) to get a space seperated string.

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 01-28-2006 22:56

Thanks all. I was so fixated on the preceding lowercase letter I didn't consider other solutions. I certainly appreciate the help.

Cheers,
DC

divinechaos
Nervous Wreck (II) Inmate

From:
Insane since: Dec 2001

posted posted 01-30-2006 01:53

FYI, preg_split() suffers the same problem as preg_replace().

However, there is now a flag (as of PHP 4.0.5) where you can include a parenthasized portion of the regex in the return. (http://ca3.php.net/manual/en/function.preg-split.php)

code:
$labelArray = preg_split("/([A-Z]+[^A-Z]*)/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
		$label = implode(" ", $labelArray);



Thanks all,
DC

« BackwardsOnwards »

Show Forum Drop Down Menu