php preg_replace - 替换社交的一部分

发布于 2024-12-10 14:57:03 字数 1036 浏览 0 评论 0原文

我想找到任何模式匹配: ###-##-####

并将 ###-##, 替换为 *** -**

但保留 -####

我在下面尝试了此操作,但根本没有任何内容被替换。

preg_replace('/(^[\d]{3})(-)([\d]{2})(-[\d]{4}$)/','\2\4',$myText);

感谢任何帮助


更新,在尝试了下面的一些建议之后,这是我当前的整个代码字符串。我正在将第二个回声输出与第一个进行比较......并且社交号码全部保持不变。

另外,正如下面提到的,我的字符串确实不仅仅包含社交...它有数千个字符长。我认为这是我真正的问题。抱歉,如果我一开始没有弄清楚这一点。

    //Make the CSC credit report request.
    $strCscResponse = $Csc->makeRequest($strFixedFormatRecord);

    echo "<br/><br/><pre>" . $strCscResponse  . "</pre><br/><br/>";

    $strCscResponse = str_replace("!", " ", $strCscResponse);

    $strCscResponse = preg_replace('/^\d{3}-\d{2}(-\d{4})$/','***-**$1',$strCscResponse);

    echo "<br/><br/><pre>" . $strCscResponse  . "</pre><br/><br/>";

更新

我想标记所有答案和“答案”,只是因为我没有澄清该字符串不仅仅包含社交内容。感谢您对这个问题的帮助,令人尴尬的是,它已经让我发疯了几天了。

I want to find any pattern matching: ###-##-####

and replace the ###-##, with ***-**

but leave the -####

I tried this below, but nothing is being replaced at all.

preg_replace('/(^[\d]{3})(-)([\d]{2})(-[\d]{4}$)/','\2\4',$myText);

Any help is appreciated


Update, here is my entire code string as it currently stands, after trying a few of the suggestions below. I am comparing the second echo output to the first... and the social numbers all remain the same.

Also, as it was mentioned below, my string does contain more than just a social... it is thousands of characters long. which i think is my real issue. Sorry if i didnt clear that up in the beginning.

    //Make the CSC credit report request.
    $strCscResponse = $Csc->makeRequest($strFixedFormatRecord);

    echo "<br/><br/><pre>" . $strCscResponse  . "</pre><br/><br/>";

    $strCscResponse = str_replace("!", " ", $strCscResponse);

    $strCscResponse = preg_replace('/^\d{3}-\d{2}(-\d{4})$/','***-**$1',$strCscResponse);

    echo "<br/><br/><pre>" . $strCscResponse  . "</pre><br/><br/>";

update

I'd like to mark all the answers and "the answer" just because i didnt clarify the string has more than just a social in it. thank you for the help with this issue, embarrisingly enough it has been driving me wild for a couple days now.

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

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

发布评论

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

评论(4

窗影残 2024-12-17 14:57:03

有一个可能的问题:您可能没有匹配正确的字符串(如果您试图找到埋在一大块文本中的 SSN) - ^$ 锚点只会匹配字符串的开头(或者有时是行的开头) - 如果这不是您想要的,而是您想要在长字符串中查找 SSN,则需要删除这些锚点。

另一个潜在的问题是,您似乎想用星号替换某些内容,但您的替换表达式中没有包含星号。你需要使用替换表达式,例如

`***-**\4`

There is one possible problem: you might not be matching the right string (if you are trying to find SSNs buried in a large block of text) - the ^ and $ anchors will only match beginning of string (or sometimes beginning of line) - if this is not what you want, but instead you want to find SSNs in a long string, you need to get rid of those anchors.

The other problem, potentially, is that you seem to want to replace things with asterisks, but you do not include asterisks in your replacement expression. you need to use a replacement expression like

`***-**\4`
枯寂 2024-12-17 14:57:03

试试这个正则表达式:

(\d{3})(-)(\d{2})(-\d{4})

Try this regex:

(\d{3})(-)(\d{2})(-\d{4})
人生戏 2024-12-17 14:57:03

试试这个:

preg_replace('/^\d{3}-\d{2}(-\d{4})$/','***-**$1',$myText);

Try this:

preg_replace('/^\d{3}-\d{2}(-\d{4})$/','***-**$1',$myText);
所有深爱都是秘密 2024-12-17 14:57:03
  1. 你的模式中有 ^ 和 $,但我没有看到 m 修饰符,所以这个
    仅当 ###-##-#### 是整个字符串时才匹配。
  2. [\d] 可以是
    缩短为 \d
  3. 你的 \2\4 将离开 --####,如果你想要 *-####
    你可以简单地拥有 *\4
  1. you have ^ and $ in your pattern, but I see no m modifier, so this
    will only match if ###-##-#### is the entire string.
  2. [\d] can be
    shortened to \d
  3. your \2\4 will leave --####, if you wanted *-####
    you can simply have *\4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文