Extract a Substring Between Two Characters in a String PHP

How to get a substring between two strings in PHP?

If the strings are different (ie: [foo] & [/foo]), take a look at this post from Justin Cook.
I copy his code below:

function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}

$fullstring = 'this is my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');

echo $parsed; // (result = dog)

Extract a substring between two characters in a string PHP

use this code

$input = "[modid=256]";
preg_match('~=(.*?)]~', $input, $output);
echo $output[1]; // 256

working example http://codepad.viper-7.com/0eD2ns

Extract a string between two characters

If you can be sure that the string format is consistent, a simple regular expression will do the trick:

$input = 'Fname Lname<fname@urmail.com>';
preg_match('~<(.*?)>~', $input, $output);
$email = $output[1];

How to extract string between special characters?

Try using this:

$string = 'This is (Name). I live in (Turkey). and so on with other brackets as well';

preg_match_all('/\((.*?)\)/i', $string, $matches);

print_r($matches[1]);

PHP : How to select specific parts of a string

You should use preg_match with regex CN=(\w+_\w+) to extract needed parts:

$strs = [
"CN=CMPPDepartemental_Direction,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan",
"CN=CMPPDepartemental_Secretariat,OU=1 - Groupes de sécurité,OU=CMPP_Departementale,OU=Pole_Ambulatoire,OU=Utilisateurs_ADEI,DC=doadei,DC=wan"
];

foreach ($strs as $str) {
$matches = null;

preg_match('/CN=(\w+_\w+)/', $str, $matches);

echo $matches[1];
}

How to Extract substrings between two special characters in php or MySQL?

Consider using explode(). Your code will be like:

$strings_arr = explode("+", $mysql_result);

So, the Array will contain:

$strings_arr[0] = 'string1';
$strings_arr[1] = 'string1';
$strings_arr[2] = 'string1';

And so on. This will actually help you: php.net/manual/en/function.explode.php

And if the unexploded string starts with +, then you can use a limit in order to remove the first empty value

Regex extract characters between two string including delimiters

You may extract those substrings between ( and ) using

preg_match('~.*\K\([^()]*\)~s', $s, $matches)

See the regex demo.

Details

  • .* - any 0+ chars, as many as possible
  • \K - match reset operator that discards the text matched so far from the match buffer
  • \( - a ( char
  • [^()]* - 0+ chars other than ( and )
  • \) - a ) char.

Get substring between two strings PHP - Reading HTML

I think its because you are declaring text as a local variable inside for loop. so , after when you are assigning $text to fullstring It's actually null. I don't understand what you are trying to do , but do this and see if it works

$fullstring = ""
foreach ($tags as $tag){
#your code as usual
echo($text);
$fullstring = $fullstring.$text;
}

and delete the $fullstring = $text line.

Get string between two strings

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];
}


Related Topics



Leave a reply



Submit