PHP 中的子字符串或正则表达式
我有以下字符串:
\r\n-----------------------------7dbbb1a140240\r\nContent-Disposition: form-data; **name="pceq"\r\n\r\n10154**
如何提取 name 之后引号中的所有内容并提取 \r\n\r\n 之后的所有内容。所以我只想提取 pceq 和 10154 。
顺便说一句,该字符串中唯一静态的内容是“破折号”和“\r\nContent-Disposition:form-data;”我不太关心这一点,我只关心名称(pceq)后面的内容,并且只关心(10154)。
非常感谢您的帮助。
更新:
我把星星去掉了(这意味着他们要加粗它)。
\r\n-----------------------------7dbbb1a140240\r\nContent-Disposition: form-data; name="pceq"\r\n\r\n10154
我喜欢@user635522 和@diEcho 方法。两个答案相等吗?这意味着他们会给出相同的结果?
我忘记提及的另一件事是我想用空字符串(“”)替换我提到的整个字符串。所以我想我需要用类似 preg_replace 或 Replace 的东西来将其删除?对此应采取什么方法?非常感谢所有回答的人。
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 extract 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果字符串存储在 $var 中,我会更喜欢使用 preg_match
I will prefer to use preg_match if string is stored in $var then
我会为此使用 strpos
I would just use strpos for this
不要使用
/"(.*)"/
匹配 name 属性的值,因为这会匹配它可以找到的第一个和最后一个 " 之间的任何内容,包括其他 "。更好的方法是使用否定字符类,如下所示:
/"[^"]*"/
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