Jump to content

parse an string by a character


etrader

Recommended Posts

I have a $string with this structure "word-word2 something-SECONDPART". I want to divide it to $string1 and $string2 by separating with the last "-".

 

The point is that "-" exists in the first part, and I just want to make the second string after the last "-"

Link to comment
Share on other sites

Is it possible for there to be a total number of hyphens in the string other than 2?

 

$string = "word- word2- - - - - -   -  something -SECONDPART";
if( strpos($string, '-') !== FALSE ) {
//$string = "wordword2 something SECONDPART";
$substr1 = substr( $string, 0, (strrpos($string, '-') ) );
$substr2 = substr( $string, (strrpos($string, '-') + 1) );
} else {
$substr1 = $string;
$substr2 = '';
}
echo "<br>First part: $substr1<br>Second part : $substr2";

Link to comment
Share on other sites

$tmp = explode('-', $string);
$string1 = $tmp[0] . '-' . $tmp[1];
$string2 = $tmp[2];
unset($tmp);

 

It works well; but a question out of curiosity. What is the role of unset?

 

 

Is it possible for there to be a total number of hyphens in the string other than 2?

 

Yes, we deal with the last "-"; it may happen several times.

 

Link to comment
Share on other sites

Actually, I need to split the string to two new ones, as I can handle both of them (Yes, I need both of them).

 

The solution provided by JakeTheSnake3.0 was very simple and thus attracted my attention; however, it only works for the first part (returning the string before the last "-").

Link to comment
Share on other sites

The code I posted above works, but I actually forgot all about array_pop. This code will work just as well.

 

$string = "This is- the str - ing for- test-ing and -doing stuff -SECONDPART"; // Test string

$arr = explode('-', $string);
$suffix = array_pop($arr);
$prefix = implode('-', $arr);

echo "<br>Prefix: $prefix<br>Suffix: $suffix"; // Everything b4 last hyphen is in $prefix, everything after in $suffix.

Link to comment
Share on other sites

Maybe?

 

  $string = "This is- the str - ing for- test-ing and -doing stuff -SECONDPART"; // Test string
  echo "<br />String 1 :" . substr($string,0,strrpos($string, '-'));
  echo "<br />String 2 :" . substr($string,strrpos($string, '-')+1);

 

scratch this... I didn't see Pika reply #2

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.