Today, I was working w
ith some code related to CSV files and converting them to xml files (will talk about xml parsing and conversion later in other posts). I needed a function through which I could get a string from a string by giving a start needle and an end needle.
For example: Given a string variable like: $string = “I am a [string variable]“; and you want to fetch out everything which lies between “[" and "]“. You can use these functions given below to accomplish this task. So you will get the result as “string variable”.
I searched PHP in-built functions libraries and I was under impression that there must be a method available under the PHP strings functions library, but didn’t find any suitable function which serves this purpose.
Though, I found couple of functions written by contributors on php.net in their comments. Thanks for their contributions, they saved my work and may help you with these functions in your php coding.
First Method: Written by administrador(ensaimada)sphoera(punt)com
Second Method: Written by heavyraptor. It returns an array with all the matches found. If there are no matches, false is returned.
Do you know more alternatives? Please share with us in comments.