5

I have strings like this:

$string1 = '35 Hose & Couplings/350902 GARDEN HOSE COUPLING, PVC, 16\/19 MM"';

$string2 = '35 Hose & Couplings/350904 GARDEN HOSE TAP CONNECTOR, PVC, 3\/4" FEMALE THREAD"';

I tried to separate the string and turn it into an array like this:

$name1 = explode('/', $string1);
$name1 = trim(end($name1));
$name2 = explode('/', $string2);
$name2 = trim(end($name2));

/*
 #results
 $name1[0] = '35 Hose & Couplings';
 $name1[1] = '350902 GARDEN HOSE COUPLING, PVC, 16\';
 $name1[2] = '19 MM"';
 ...
 #expected results
 $name1[0] = '35 Hose & Couplings';
 $name1[1] = '350902 GARDEN HOSE COUPLING, PVC, 16\/19 MM"';
 ...
*/

I want to explode the string when there is / only, but when it meets \/ it shouldn't explode the string, my code still explode the string if it contains \/, is there a way to do this?

5
  • 'i want to explode the string when there is '/' only, but when it meets '/' it will not explode the string'- what do you mean ? Commented Feb 26, 2018 at 10:48
  • @IsThisJavascript Yes that's what i want Commented Feb 26, 2018 at 10:50
  • have you looked at preg_split (php.net/manual/de/function.preg-split.php) Commented Feb 26, 2018 at 10:51
  • I think this is a job for regex, unfortunately my regex skills suck. Maybe you could try looking here for further assistance until someone can provide better guidance regexr.com Commented Feb 26, 2018 at 10:52
  • 1
    You an either first "protect" the exception "\\/" by replacing it with some "magical" sequence and convert it back after having replaced the remaining "/" occurrances, or you use a regular expression with lookahead. Commented Feb 26, 2018 at 10:54

2 Answers 2

3

You could use a regular expression with negative look-behind:

$parts = preg_split('~(?<!\\\\)/~', $string1);

See example on eval.in

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

3 Comments

I like that solution. It does the job and the regex looks like a fish.
It would be nice if you provide a live demo in your answer.
@revo, as far as I know SO does not have possibilities for live PHP demos within answers -- as exists for JavaScript. But I added a link.
1

You can go like this:

$string1 = str_replace("\/","@",$string1);
$name1 = explode('/', $string1);
foreach($name1 as $id => $name) {
    $name1[$id] = str_replace("@","\/",$name1[$id]);
}

a little cumbersome, I know, but should do the trick. Wrap it in a function for better readability.

Basically I replaced the string you do not want to explode by with a temporary string and reverted it back after exploding.

8 Comments

Then use something else, like # or whatever. Code needs to be adjusted to a project, always.
You are right, I copied that code fragment from the question. I removed it now.
@MichałSkrzypek it prints 350902 GARDEN HOSE COUPLING, PVC, 16@19 MM" the @ is replacing the \/ , there'll be a problem if i want to use @
Yes, it was suggested above, I advised to use "#" or whatever sign you will not need instead. The foreach loop should revert @ back to \/.
Although I believe regex is the way forward with this; you could also user multiple symbols that you would never have in sequence. @#]! for 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.