extract string between


/ Published in: PHP
Save to your folder(s)

Two functions. 1) Find the string between characters. 2) Create array of strings found between two characters.


Copy this code and paste it in your HTML
  1. function TextBetweenArray($s1,$s2,$s){
  2. $myarray=array();
  3. $s1=strtolower($s1);
  4. $s2=strtolower($s2);
  5. $L1=strlen($s1);
  6. $L2=strlen($s2);
  7. $scheck=strtolower($s);
  8. do{
  9. $pos1 = strpos($scheck,$s1);
  10. if($pos1!==false){
  11. $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
  12. if($pos2!==false){
  13. $myarray[]=substr($s,$pos1+$L1,$pos2);
  14. $s=substr($s,$pos1+$L1+$pos2+$L2);
  15. $scheck=strtolower($s);
  16. }
  17. }
  18. } while (($pos1!==false)and($pos2!==false));
  19. return $myarray;
  20. }
  21.  
  22. function TextBetween($s1,$s2,$s){
  23. $s1 = strtolower($s1);
  24. $s2 = strtolower($s2);
  25. $L1 = strlen($s1);
  26. $scheck = strtolower($s);
  27. if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;}
  28. if($pos1 !== false){
  29. if($s2 == '') return substr($s,$pos1+$L1);
  30. $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
  31. if($pos2!==false) return substr($s,$pos1+$L1,$pos2);
  32. }
  33. return '';
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.