php preg_replace - 替换社交的一部分
我想找到任何模式匹配: ###-##-####
并将 ###-##,
替换为 *** -**
但保留 -####
我在下面尝试了此操作,但根本没有任何内容被替换。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个可能的问题:您可能没有匹配正确的字符串(如果您试图找到埋在一大块文本中的 SSN) -
^
和$
锚点只会匹配字符串的开头(或者有时是行的开头) - 如果这不是您想要的,而是您想要在长字符串中查找 SSN,则需要删除这些锚点。另一个潜在的问题是,您似乎想用星号替换某些内容,但您的替换表达式中没有包含星号。你需要使用替换表达式,例如
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
试试这个正则表达式:
Try this regex:
试试这个:
Try this:
仅当 ###-##-#### 是整个字符串时才匹配。
缩短为 \d
你可以简单地拥有 *\4
will only match if ###-##-#### is the entire string.
shortened to \d
you can simply have *\4