PHP 中的子字符串或正则表达式

发布于 2024-10-20 07:24:58 字数 753 浏览 3 评论 0原文

我有以下字符串:

\r\n-----------------------------7dbbb1a140240\r\nContent-Disposition: form-data; **name="pceq"\r\n\r\n10154**

如何提取 name 之后引号中的所有内容并提取 \r\n\r\n 之后的所有内容。所以我只想提取 pceq10154

顺便说一句,该字符串中唯一静态的内容是“破折号”和“\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我要还你自由 2024-10-27 07:24:58

如果字符串存储在 $var 中,我会更喜欢使用 preg_match

preg_match('/name="(.*)"\r\n\r\n(.*)/', $var, $matches);
echo $matches[1]; // name
echo $matches[2]; // numbers

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
热鲨 2024-10-27 07:24:58

我会为此使用 strpos

$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?

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?
独行侠 2024-10-27 07:24:58

不要使用 /"(.*)"/ 匹配 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: /"[^"]*"/

爱冒险 2024-10-27 07:24:58

试试这个

preg_match('/^name="(.*)"\r\n\r\n(.*)/', $var, $matches);
echo $matches[1]; // name
echo $matches[2]; // numbers

try this

preg_match('/^name="(.*)"\r\n\r\n(.*)/', $var, $matches);
echo $matches[1]; // name
echo $matches[2]; // numbers
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文