How to Get a Substring Between Two Strings in 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

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

Get content between two strings PHP

  • Use # instead of / so you dont have to escape them.
  • The modifier s makes . and \s also include newlines.
  • { and } has various functionality like from n to m times in {n,m}.
  • The basic

    preg_match('#\\{FINDME\\}(.+)\\{/FINDME\\}#s',$out,$matches);
  • The advanced for various tags etc (styling is not so nice by the javascript).

    $delimiter = '#';
    $startTag = '{FINDME}';
    $endTag = '{/FINDME}';
    $regex = $delimiter . preg_quote($startTag, $delimiter)
    . '(.*?)'
    . preg_quote($endTag, $delimiter)
    . $delimiter
    . 's';
    preg_match($regex,$out,$matches);

Put this code in a function

  • For any file which you do not want to execue any stray php code, you should use file_get_contents. include/require should not even be an option there.

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 all occurance of string between two strings in php

Refer this link

http://forums.devshed.com/php-development-5/values-string-tags-578670.html

And change your code like this,

preg_match_all("#<abc([^<]+)>#", $yourstring, $ansvariable); 
//echo implode("::", $ansvariable[1]);


foreach($yourstring[1] as $key => $val){
echo $yourstring[1][$key]."<br>"; // prints $val
}

Get the substring that is in between two strings php

explode might need a workaround because ( and ) are different. A simple regex will get the job done.

$string1 = "(substring1) Hello";
preg_match('#\((.*?)\)#', $string1, $match); // preg_match_all if there can be multiple such substrings
print $match[1];

PHP getting string in between two strings

Use the explode function from php:

$arr = explode("$",$string);

In the $arr, you will have an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the $ sign.

More info here

Get multiple matches in string between two strings - small issue in function that I use?

$string = "somerandomcharacters[code]object1[/code]somerandomcharacters[code]object2[/code]somerandomcharacters[code]object3[/code]somerandomcharacters";

preg_match_all('%\[code\](.*?)\[/code\]%i', $string, $matches, PREG_PATTERN_ORDER);

print_r($matches[1]);

Output:

Array
(
[0] => object1
[1] => object2
[2] => object3
)

Regex Explanation:

\[code\](.*?)\[/code\]

Options: Case insensitive

Match the character “[” literally «\[»
Match the character string “code” literally (case insensitive) «code»
Match the character “]” literally «\]»
Match the regex below and capture its match into backreference number 1 «(.*?)»
Match any single character that is NOT a line break character (line feed) «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “[” literally «\[»
Match the character string “/code” literally (case insensitive) «/code»
Match the character “]” literally «\]»

DEMO: http://ideone.com/wVvssx

Get string between - Find all occurrences PHP

One possible approach:

function getContents($str, $startDelimiter, $endDelimiter) {
$contents = array();
$startDelimiterLength = strlen($startDelimiter);
$endDelimiterLength = strlen($endDelimiter);
$startFrom = $contentStart = $contentEnd = 0;
while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos($str, $endDelimiter, $contentStart);
if (false === $contentEnd) {
break;
}
$contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
$startFrom = $contentEnd + $endDelimiterLength;
}

return $contents;
}

Usage:

$sample = '<start>One<end>aaa<start>TwoTwo<end>Three<start>Four<end><start>Five<end>';
print_r( getContents($sample, '<start>', '<end>') );
/*
Array
(
[0] => One
[1] => TwoTwo
[2] => Four
[3] => Five
)
*/

Demo.



Related Topics



Leave a reply



Submit