preg_split or preg_match - need to include delimiter

Posted on 2008-02-01
Medium Priority
Last Modified: 2012-06-27
Thanks in advance to whoever can help me!

I guess this might be an easy question but I'm horrible with regular expressions. I need to split all of the authors inside an XML tag by a number that follows the authors name but I ALSO NEED TO INCLUDE THE NUMBER in the match that is outputted. So something like this:

<authors>joe smith1 jane doe2 jack johnson3</authors>

array {
[0]=> joe smith1
[1]=> jane doe2
[2]=> jack johnson3

I need to do the same with affiliations but the number is in the front this time:

<affiliations>1 university of nowhere 2 ministry of silly walks 3 pcu</affiliations>

array {
[0]=> 1 university of nowhere
[1]=> 2 ministry of silly walks
[2]=> 3 pcu

How can I do this using preg_match, preg_split or some other reg ex matching solution?
Question by:sherlock5
  • 3
  • 2
LVL 27

Expert Comment

ID: 20797704

Author Comment

ID: 20798224
yeah, i tried that before but the delimiter wasn't included in the output:

$author_array = preg_split("/(\d{1,2})/", $r["Authors"], -1, PREG_SPLIT_DELIM_CAPTURE);

Array ( [0] => Joe Smith [1] => 1 [2] => Jane Doe [3] => 1 [4] => )

is there some other setting that i am missing? i need it to output like this:

Array ( [0] => Joe Smith1 [1] => Jane Doe2 )
LVL 27

Accepted Solution

ddrudik earned 500 total points
ID: 20798472
This code seems to match your data:
$authorblock='<authors>joe smith1 jane doe2 jack johnson3</authors>';
$author_array = preg_split('/([^\d]*\d{1,2} )/',preg_replace('/(?:^<authors>|<\/authors>$)/','',$authorblock), -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
echo '<pre>'.htmlentities(print_r($author_array,true));
$affliationsblock='<affiliations>1 university of nowhere 2 ministry of silly walks 3 pcu</affiliations>';
$affiliation_array = preg_split('/(\d{1,2} [^\d]*)/',preg_replace('/(?:^<affiliations>|<\/affiliations>$)/','',$affliationsblock), -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
echo '<pre>'.htmlentities(print_r($affiliation_array,true));

Open in new window


Author Closing Comment

ID: 31427169
thanks for the help! that did the trick!
LVL 27

Expert Comment

ID: 20805298
Thanks for the question and the points.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question