preg_替换特定字符之前的所有字符

发布于 2024-08-23 02:46:30 字数 589 浏览 5 评论 0原文

我有一个字符串

&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&

,我必须删除这部分 &185601651932|mobile|3|120|1& (以 amp 开头并以 amp 结尾)知道只有第一个数字直到垂直线(185601651932),

这样结果我就会有

&168491968426|mobile|3|100|1&114192088691|mobile|3|555|5&

如何使用 PHP preg_replace 函数做到这一点。行 (|) 分隔值的数量始终相同,但 id 仍然希望有一个灵活的模式,而不取决于 & 之间的行数。符号。

谢谢。

PS 另外,如果能提供一个与 php 中的正则表达式相关的简单编写的良好资源的链接,我将非常感激。谷歌上有很多这样的链接:)但也许你碰巧有一个非常好的链接

I have a string

&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&

and i have to delete, say, this part &185601651932|mobile|3|120|1& (starting with amp and ending with amp) knowing only the first number up to vertical line (185601651932)

so that in result i would have

&168491968426|mobile|3|100|1&114192088691|mobile|3|555|5&

How could i do that with PHP preg_replace function. The number of line (|) separated values would be always the same, but still, id like to have a flexible pattern, not depending on the number of lines in between the & sign.

Thanks.

P.S. Also, I would be greatful for a link to a good simply written resource relating regular expressions in php. There are plenty of them in google :) but maybe you happen to have a really great link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

无戏配角 2024-08-30 02:46:30
preg_replace("/&185601651932\\|[^&]+&/", ...)

广义而言,

$i = 185601651932;
preg_replace("/&$i\\|[^&]+&/", ...);
preg_replace("/&185601651932\\|[^&]+&/", ...)

Generalized,

$i = 185601651932;
preg_replace("/&$i\\|[^&]+&/", ...);
一枫情书 2024-08-30 02:46:30

如果您想要真正的灵活性,请使用 preg_replace_callback。 http://php.net/manual/en/function.preg-替换回调.php

if you want real flexibility, use preg_replace_callback. http://php.net/manual/en/function.preg-replace-callback.php

不知所踪 2024-08-30 02:46:30

重要提示:不要忘记使用 preg_quote()< 转义您的号码/代码>

$string = '&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$number = 185601651932;
if (preg_match('/&' . preg_quote($number, '/') . '.*?&/', $string, $matches)) {
    // $matches[0] contains the captured string
}

Important: don't forget to escape your number using preg_quote():

$string = '&168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$number = 185601651932;
if (preg_match('/&' . preg_quote($number, '/') . '.*?&/', $string, $matches)) {
    // $matches[0] contains the captured string
}
抱着落日 2024-08-30 02:46:30

在我看来,您应该使用除字符串之外的另一种数据结构来操作此数据。
我希望这些数据处于像

Array(
  [id] => Array(
     [field_1] => value_1
     [field_2] => value_2
  )
)

您的大量字符串这样的结构中,可以通过执行以下操作将其按摩到这样的结构中:

$data_str = '168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$remove_num = '185601651932';

/* Enter a descriptive name for each of the numbers here 
- these will be field names in the data structure */
$field_names = array( 
    'number',
    'phone_type',
    'some_num1',
    'some_num2',
    'some_num3'
);

/* split the string into its parts, and place them into the $data array */
$data = array();
$tmp = explode('&', trim($data_str, '&'));
foreach($tmp as $record) {
    $fields = explode('|', trim($record, '|'));
    $data[$fields[0]] = array_combine($field_names, $fields);
}

echo "<h2>Data structure:</h2><pre>"; print_r($data); echo "</pre>\n";
/* Now to remove our number */
unset($data[$remove_num]);
echo "<h2>Data after removal:</h2><pre>"; print_r($data); echo "</pre>\n";

It seems to me you ought to be using another data structure than a string to manipulate this data.
I'd want this data in a structure like

Array(
  [id] => Array(
     [field_1] => value_1
     [field_2] => value_2
  )
)

Your massive string can be massaged into such a structure by doing something like this:

$data_str = '168491968426|mobile|3|100|1&185601651932|mobile|3|120|1&114192088691|mobile|3|555|5&';
$remove_num = '185601651932';

/* Enter a descriptive name for each of the numbers here 
- these will be field names in the data structure */
$field_names = array( 
    'number',
    'phone_type',
    'some_num1',
    'some_num2',
    'some_num3'
);

/* split the string into its parts, and place them into the $data array */
$data = array();
$tmp = explode('&', trim($data_str, '&'));
foreach($tmp as $record) {
    $fields = explode('|', trim($record, '|'));
    $data[$fields[0]] = array_combine($field_names, $fields);
}

echo "<h2>Data structure:</h2><pre>"; print_r($data); echo "</pre>\n";
/* Now to remove our number */
unset($data[$remove_num]);
echo "<h2>Data after removal:</h2><pre>"; print_r($data); echo "</pre>\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文