I have the following string:
\r\n-----------------------------7dbbb1a140240\r\nContent-Disposition: form-data; **name="pceq"\r\n\r\n10154**
How would I go about extracting anything that's in the quote right after name and extract everything after \r\n\r\n. So I want to ext开发者_如何学Goract pceq and 10154 only.
BTW the only thing that's static in this string would be "dashes" and "\r\nContent-Disposition: form-data;" Which I can care less, I only care about whatever is right after name (pceq) and only (10154).
Your help is much appreciated and thanks.
Update:
I took the stars out (which meant to be their to bold it).
\r\n-----------------------------7dbbb1a140240\r\nContent-Disposition: form-data; name="pceq"\r\n\r\n10154
I like @user635522 and @diEcho approach. Are both answer equal? Meanning they'll give the same result?
Another thing I forgot to mention is that I would like to replace the entire string that I mentioned with an empty string(""). So something like preg_replace or replace I guess I need to use to blank it out? What would be the approach for that? Thanks a lot for everyone who answered.
I will prefer to use preg_match if string is stored in $var then
preg_match('/name="(.*)"\r\n\r\n(.*)/', $var, $matches);
echo $matches[1]; // name
echo $matches[2]; // numbers
I would just use strpos for this
$searchstr; // the string to be searched
$first = substr($searchstr,strpos($searchstr,"name=")+5,strpos($searchstr,'"\r\n"));
$second = substr($searchstr,strpos($searchstr,"\r\n\r\n")+8); // offset here might be 4?
Do not match for the value of the name attribute using /"(.*)"/
as this would match anything between the first and last " it can find, including other ".
A better approach is using negated character classes like so: /"[^"]*"/
try this
preg_match('/^name="(.*)"\r\n\r\n(.*)/', $var, $matches);
echo $matches[1]; // name
echo $matches[2]; // numbers
精彩评论