0

Is there a function in php to search a string and find a sub string based on it being between two defined character sets?

I am trying to search through a few files using a php script and grab a string from them.

the string i need is a URL, each file has a string that looks like this with a different url inside

/A << /URI (https://www.website/custom_ url_per_file)
/S /URI >>    //an actual linebreak in the code

I have all the files opening in a foreach loop, so how can i extract the url between the 2 strings $a and $b?

$a = /A << /URI
$b = /S /URI >>  
1
  • You're looking for "non-greedy capturing group": /$prefix(.+?)$suffix/. Commented Sep 23, 2014 at 10:04

1 Answer 1

1
<?php

$string = '/A << /URI (https://www.website/custom_ url_per_file)
/S /URI >>    //an actual linebreak in the code';

preg_match('#/A << /URI \(([^\)]+)\)\s+\/S /URI >>#', $string, $matches);

print_r($matches);

?>

will result to:

Array
(
[0] => /A << /URI (https://www.website/custom_ url_per_file)
/S /URI >>
[1] => https://www.website/custom_ url_per_file
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.