Jump to content

get the text between 2 words in a query


anarchoi

Recommended Posts

try[code]<?php
$test ='bla bla text bla bla <yo> 1st texr <yo2> bla bla textbla bla text bla bla <yo> 2nd text <yo2> bla bla textbla bla text bla bla <yo> 3rd text <yo2> bla bla text';
$a = preg_split('/<yo>/',$test);
for ($i = 1; $i < count($a); $i++) {
$b = preg_split('/<yo2>/',$a[$i]);
$c[] = $b[0];
}
print_r($c);
?>[/code]
Here is a call-able function
[code]
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);
}
[/code]
[code=php:0]<?php

$string = "bla bla text bla bla <yo>text text<yo2> bla bla text
bla bla text bla bla <yo>some more text<yo2> bla bla text";

preg_match_all('/(?<=<yo>).*?(?=<yo2>)/s', $string, $matches);
$matches = $matches[0];

print_r($matches);

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.