Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string between two strings

My string is: "reply-234-private", i want to get the number after "reply-" and before "-private", it is "234". I have tried with following code but it returns an empty result:

$string = 'reply-234-private';
$display = preg_replace('/reply-(.*?)-private/','',$string);
echo $display;
like image 233
Thanh Nguyen Avatar asked Jan 14 '13 03:01

Thanh Nguyen


People also ask

How do you get a string between two strings in Python?

To find a string between two strings in Python, use the re.search() method. The re.search() is a built-in Python method that searches a string for a match and returns the Match object if it finds a match.

How do I extract a string between two symbols?

If you want to extract part string between two same characters, you can do as this: Select a cell which you will place the result, type this formula =SUBSTITUTE(MID(SUBSTITUTE("/" & A3&REPT(" ",6),"/",REPT(",",255)),2*255,255),",",""), and press Enter key.


6 Answers

You can just explode it:

<?php
$string = 'reply-234-private';
$display = explode('-', $string);

var_dump($display);
// prints array(3) { [0]=> string(5) "reply" [1]=> string(3) "234" [2]=> string(7) "private" }

echo $display[1];
// prints 234

Or, use preg_match

<?php
$string = 'reply-234-private';
if (preg_match('/reply-(.*?)-private/', $string, $display) === 1) {
    echo $display[1];
}
like image 188
sachleen Avatar answered Nov 12 '22 15:11

sachleen


Have a look at explode() function

something like this:

$myString = 'reply-234-private';

$myStringPartsArray = explode("-", $myString);

$answer = $myStringPartsArray[1];
like image 32
Chris Jackson Avatar answered Nov 12 '22 14:11

Chris Jackson


This article show you, How to get of everything string between two tags or two strings.

http://okeschool.com/articles/312/string/how-to-get-of-everything-string-between-two-tag-or-two-strings

<?php
 // Create the Function to get the string
 function GetStringBetween ($string, $start, $finish) {
    $string = " ".$string;
    $position = strpos($string, $start);
    if ($position == 0) return "";
    $position += strlen($start);
    $length = strpos($string, $finish, $position) - $position;
    return substr($string, $position, $length);
}
?>

in case your question, you can try this :

$string1='reply-234-private';
echo GetStringBetween ($string1, "-", "-")

or we can use any 'identifier string' for grab the string between the identifier string. for example:

echo GetStringBetween ($string1, "reply-", "-private")
like image 20
Kuncara Kurniawan Avatar answered Nov 12 '22 16:11

Kuncara Kurniawan


Use php's inbuilt regex support functions preg_match_all

this would help, suppose you want the array of strings(keys) between @@ in following example, where '/' doesn't fall in-between, you can built new example with different start an end variable

function getInbetweenStrings($start, $end, $str){
    $matches = array();
    $regex = "/$start([a-zA-Z0-9_]*)$end/";
    preg_match_all($regex, $str, $matches);
    return $matches[1];
}

$str = "C://@@ad_custom_attr1@@/@@upn@@/@@samaccountname@@";
$str_arr = getInbetweenStrings('@@', '@@', $str);

print_r($str_arr);
like image 33
Ravi Verma Avatar answered Nov 12 '22 16:11

Ravi Verma


$myString = 'reply-234-private';
echo str_replace('-','',filter_var($myString,FILTER_SANITIZE_NUMBER_INT));

That should do the job.

like image 29
szikael Avatar answered Nov 12 '22 14:11

szikael


If you want to do it in js, try this function -

function getStringBetween(str , fromStr , toStr){
  var fromStrIndex = str.indexOf(fromStr) == -1 ? 0 : str.indexOf(fromStr) + fromStr.length;
  var toStrIndex = str.slice(fromStrIndex).indexOf(toStr) == -1 ? str.length-1 : str.slice(fromStrIndex).indexOf(toStr) + fromStrIndex;
  var strBtween = str.substring(fromStrIndex,toStrIndex);
  return strBtween;
}
like image 42
user2951807 Avatar answered Nov 12 '22 14:11

user2951807