Split a string into two strings

Posted on 2009-07-13
Last Modified: 2013-12-13
I have a string that returns pairs of data i.e. (name 1,url1) and there can be multiple pairs.
Sometimes instead of the URL there is an error message. How can I separate the ones with the url form the others into two strings?
Question by:NGTLD
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24840085
A good approach would be ....

1.  explode() the string into an array.  The exact code depends on the format of the original string and what separates the actual pairs of name/url/error.

2.  Loop through the array in a fashion that processes it by pairs.  

(a)   For odd-numbered index, you will get the name.  For even-numbered index, you will get either a URL or the error message.

(b)  For each even-numbered index, use any of the standard methods to tell if it is a valid URL.  If not, it's an error message.

(c)  Store the resulting name/URL or name/errormsg in whatever form you need.

Author Comment

ID: 24846091
Maybe I misunderstand, but what I want to do is split out the ones where the url is not a url.
LVL 19

Expert Comment

ID: 24847109
is the data in an array?

if so you could do a loop like foreach or while

Can you post your code please.

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24848116
Show me how your original string looks in detail.  Post an example:

$origstring = "(n1,url1)(n2,error2)(n3,url3)"  
 Something like this?

Author Comment

ID: 24849613
First I goofed it is an array, sorry here is the output
Array ( [0] => 3675795;results/3675795.pdf [1] => 20040123456;FAILED )

It's the ones like the failed I want to separate into a second array
LVL 27

Accepted Solution

Cornelia Yoder earned 500 total points
ID: 24849835
Already being in an array makes it easier.

$array = array("3675795;results/3675795.pdf","20040123456;FAILED","xxx;FAILED","12345;results/12345.pdf");
$goodarray = array();
$failedarray = array();
for ($i=0;$i<count($array);$i++)
  $pairarray = explode(";", $array[$i]);
  if ($pairarray[1] == "FAILED")
    $failedarray[] = "$pairarray[0];$pairarray[1]";
    $goodarray[] = "$pairarray[0];$pairarray[1]";
print_r ($array);
echo "<BR>";
print_r ($goodarray);
echo "<BR>";
print_r ($failedarray);

Open in new window


Author Comment

ID: 24865331
I think it is close, although I am not sure what is wrong I get he following output  

Array ( [0] => 3675795;results/3675795.pdf [1] => A15;Reference Number Invalid or Not Yet Supported )
Array ( )

Before the tt are the values that are in $respass. THe second one should be in $resfail.  I am attaching the code as I implemented it.
			$respass = array();
			$resfail= array();
for ($i=0;$i<count($results);$i++)
  $pairarray = explode(";", $results[$i]);
  if ($pairarray[1] == "Reference Number Invalid or Not Yet Supported ")
    $resfail[] = "$pairarray[0];$pairarray[1]";
    $respass[] = "$pairarray[0];$pairarray[1]";
	print_r ($respass);
echo "<BR> tt <BR>" ;
print_r ($resfail);

Open in new window


Author Comment

ID: 24865349
it worked I had an extra space in there. Thanks !!!!

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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