0

I have a string like this one

$sitesinfo = 'site 1 titles-example1.com/
site 2 titles-example2.com/
site 3 titles-example3.com/
site 4 titles-example4.com/
site 5 titles-example5.com/';

I used to split lines into 1 array like this

$lines = explode("/",$sitesinfo);

then while I loop I got each line into an array object without problems.

What do I need to do to split each line into 2 pieces and add each piece into an array so the result be like this

$titles = array("site 1 titles","site 2 titles","site 3 titles","site 4 titles","site 5 titles");
$domains = array("example1.com","example2.com","example3.com","example4.com","example5.com");

so I can use them in script.

1
  • Where does this string come from? This is a relevant detail because PHP has dedicated functions for parsing lines of file contents. fscanf($handle, '%[^-]-%[^/]', $titles[], $domains[]) comes to mind. Commented Sep 28, 2024 at 8:28

3 Answers 3

2

How about this:

foreach ($lines as $line) {
  $t = explode('-', trim($line), 2);
  if (count($t) < 2) {
    echo "Failed to parse the line $line";
    continue;
  }
  $titles[] = $t[0];
  $domains[] = $t[1];
}

Explanation: each line split by '-' symbol into exactly 2 parts (if there's less, that's an error - the line doesn't contain '-', and thus shouldn't be processed at all). The first part is pushed into $titles array, the second - into $domains.

Sign up to request clarification or add additional context in comments.

5 Comments

sorry for the downvote, but I don't like this solution because it assumes exploding at "/" like what OP had, and goes from there. What happens when a URL doesn't end in "/"? It would fail if the URL doesn't end in that, and even if it does, it doesn't put it back on, since it is part of the URL. At a minimum, should be exploded at the line break (if you add this to your post then i'll give you vote back :) )
well you didn't show where $lines is coming from, so it is assumed you started your code where the OP left off. If that is not what $lines represents in your code, then show what $lines is coming from.
And, by the way, the domains the OP wished to see (in his example) are without the ending '/' symbol... Well, whatever.
excuse me, but you did not offer a complete solution. Your code starts with the assumption that $lines is an array of rows from the OP's $sitesinfo. Since part of the problem involves a single string, your code either a) skips a step, or b) starts where OP left off with that explode. Instead of getting your panties in a wad over this fact, fix your code and get your vote back. It's that simple.
1

regex alternative:

$sitesinfo = 'site 1 titles-example1.com/
site 2 titles-example2.com/
site 3 titles-example3.com/
site 4 titles-example4.com/
site 5 titles-example5.com/';

preg_match_all('~([^-]+)-(.*)~',$sitesinfo,$matches);
$titles = array_map('trim',$matches[1]);
$domains = array_map('trim',$matches[2]);

Comments

1

First of all split the string into each line (by the line ending character).

Then split each line into two parts at the first - and add those parts to the result. You can later on give each part a name on it's own:

Example/Demo:

$lines = explode("\r\n", $sitesinfo);
$r = array(null, null);
foreach($lines as $line) {
    list($r[0][], $r[1][]) = explode("-", $line, 2) + array(1 => NULL);
}
list($titles, $domains) = $r;
unset($r);

Or the regex variant (Demo):

preg_match_all('~([^-]+)-(.*)~', $sitesinfo, $matches);
list(, $titles, $domains) = $matches;
unset($matches);

Result:

array(5) {
  [0]=>
  string(13) "site 1 titles"
  [1]=>
  string(13) "site 2 titles"
  [2]=>
  string(13) "site 3 titles"
  [3]=>
  string(13) "site 4 titles"
  [4]=>
  string(13) "site 5 titles"
}
array(5) {
  [0]=>
  string(13) "example1.com/"
  [1]=>
  string(13) "example2.com/"
  [2]=>
  string(13) "example3.com/"
  [3]=>
  string(13) "example4.com/"
  [4]=>
  string(13) "example5.com/"
}

2 Comments

+1. ) I rarely use list operator (may be because it's too similar, but not quite to ($foo, @bar...) = Perl construct, and I'm real afraid of bugs that might be introduced by that not-quite-similarity), but I enjoy seeing others using it. )
@raina77ow: Yeah, can be useful, added another example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.