English 中文(简体)
需要定期表达
原标题:Need a regular expression
  • 时间:2011-11-14 21:37:49
  •  标签:
  • php
  • regex

例如,我去街面“1234 West 23rd Street”。 我只想打上第一个街号,在每一街号之间放下空间,如“1 2 3 4 West 23rd Street”。

Using the simple expression like "([0-9])" and replacing it with "$1 " almost works, except I want it to stop after the first space and not match, per the above example, the "23" in "23rd" as well.

最佳回答

This is possible with preg_replace and the pattern modifier e , which:

replacement str str in substitution substitution substitution of as as as as

$str =  1234 West 23rd Street ;
preg_replace( /^([[:digit:]]+)/e , "implode(   , str_split( $1 ))", $str);
// returns  1 2 3 4 West 23rd St 
问题回答

你再次需要PHP帮助你来到这里,你只能与Regex一起这样做(Regex不会像这种情况那样做,而是没有为之设计)。

与此相匹配:([0-9]+]s,然后由您获得(如果只有一个以上,可能只是使用第一个结果),添加了您在每种特性之间所希望的空间。

因此,你只想对位数和空间之后的数值进行对比。 您可使用<代码><>>>/代码>,与线的开始相吻合。 因此,你可以研究这样的情况:

^(d+)

<代码>d与任何数字对应。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签