-3

for example: I want to get a specific text from this text after the word investor and before the word harga

abstrak investor evaluasi analisis faktor mempengaruhi nilai intrinsik harga saham

so the result would be

evaluasi analisis faktor mempengaruhi nilai intrinsik

but in the other case, I also have string, and I want to get a text after the word tersedia and before the word timbul

informasi tersedia investor evaluasi analisis faktor mempengaruhi kondisi perusahaan dimasa investor memperkecil kerugian timbul seminimal fluktuasi pertumbuhan perkembangan perusahaan

so the result would be

investor evaluasi analisis faktor mempengaruhi kondisi perusahaan dimasa investor memperkecil kerugian

I've use this code, to get a text after and before a certain word, but it only has two fixed parameter, the "start word" and the "end word". Is it possible to make it more dynamic or on the other word add rule/more than two parameters?

<?php 
    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)

?>

Any helps would be appreciated. Thank you.

Juli Andika
  • 57
  • 10

4 Answers4

0

You can just loop through all your start/end words:

$words = [
    ['start' => 'Start word 1', 'end' => 'End word 1'],
    ['start' => 'Start word 2', 'end' => 'End word 2'],
    ['start' => 'Start word 3', 'end' => 'End word 3'],
    // ...
];

$foundText = '';
foreach ($words as $w) {
    if ($foundText = get_string_between($fullstring, $w['start'], $w['end'])) {
        break;
    }
}

var_dump($foundText);
brevis
  • 1,191
  • 10
  • 15
0

You can use substr and strpos to strip out the part you want.

$first = "investor";
$last = "harga";

$str = "abstrak investor evaluasi analisis faktor mempengaruhi nilai intrinsik harga saham";

Echo substr($str, strpos($str, $first)+strlen($first), strpos($str, $last)-strpos($str, $first)-strlen($first));

Strpos finds the position of the word you search for.
Strlen gets the lenght of the string.

https://3v4l.org/vIWYs

Echo substr($str, 
    strpos($str, $first) //find position of investor
    +strlen($first) // add lenght of investor to "skip it" in the output
    , strpos($str, $last) // find position of harga
    -strpos($str, $first) // subtract the number of characters between string start and investor
    -strlen($first) // subtract lenght of investor
    );

This can off course be used on both of your strings https://3v4l.org/TutAG

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

You can do like this, First of all you have to remove extra from beginning like,

$str = "abstrak investor evaluasi analisis faktor mempengaruhi nilai 
intrinsik harga saham";

$start = "investor";
$end = "harga";
$r = explode($start, $str);

in r[1] you get resulting string, then after same as you have remove extra from end. like,

if (isset($r[1])){
   $r = explode($end, $r[1]);
   echo $r[0];
}

Here in r[0] you get resulted string.
I hope it helps, check this demo link

Ritesh Khatri
  • 1,253
  • 13
  • 29
0
 # use PREG_OFFSET_CAPTURE in preg_match_all() function to obtain an 
    # array *$matchedpairs* of two element multi-dimensional arrays of all the matches paired with
    # the start offset of the matched string
    $fullstring = "abstrak investor evaluasi abstrak analisis faktor mempengaruhi nilai intrinsik harga saham perusahaan meningkatkan pengambilan keputusan";
    preg_match_all('/(\babstrak\b){1}(\w|\s)+(\bpengambilan\b){1}/', (''.$fullstring.'') , $matchedpairs , PREG_OFFSET_CAPTURE);