• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Split a string into two strings

Hi,
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?
0
NGTLD
Asked:
NGTLD
  • 4
  • 3
1 Solution
 
Cornelia YoderArtistCommented:
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.
0
 
NGTLDAuthor Commented:
Maybe I misunderstand, but what I want to do is split out the ones where the url is not a url.
0
 
NerdsOfTechTechnology ScientistCommented:
is the data in an array?

if so you could do a loop like foreach or while

Can you post your code please.

Thanks
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Cornelia YoderArtistCommented:
Show me how your original string looks in detail.  Post an example:

$origstring = "(n1,url1)(n2,error2)(n3,url3)"  
 Something like this?
0
 
NGTLDAuthor Commented:
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
0
 
Cornelia YoderArtistCommented:
Already being in an array makes it easier.


<?php
 
$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]";
  else
    $goodarray[] = "$pairarray[0];$pairarray[1]";
}
 
print_r ($array);
echo "<BR>";
print_r ($goodarray);
echo "<BR>";
print_r ($failedarray);
 
exit;
?>

Open in new window

0
 
NGTLDAuthor Commented:
Hi,
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 )
tt
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]";
  else
    $respass[] = "$pairarray[0];$pairarray[1]";
}
		
	print_r ($respass);
echo "<BR> tt <BR>" ;
print_r ($resfail);

Open in new window

0
 
NGTLDAuthor Commented:
Nevermind,
it worked I had an extra space in there. Thanks !!!!
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now