标签: preg-replace

使用数组多次替换(可能是preg_replace)相同的字符串

我需要用数组中的字符串替换某个字符串(问号)的多个实例.例如,如果我想要替换的字符串出现3次而我的数组长度为3,则第一个将被数组中的第一个项目替换,第二个将替换为第二个等等.

你可能会认识到它与mysqli中预处理语句的工作方式非常相似.

这是一个例子:

$myArray = array(
    [0] => 'yellow',
    [1] => 'green',
    [2] => 'red'
);

$myString = 'banana is ?, apple is ?, tomato is ?';

$newString = someFunction($myString,$myArray);

echo $newString;

然后这将返回

banana is yellow, apple is green, tomato is red

任何人都可以使用PHP 5.2建议一种方法.

php preg-replace

1
推荐指数
1
解决办法
1325
查看次数

替换链接位置(href ='...')

我想替换页面的链接位置(锚标记),如下所示.

样本输入:

text text text <a href='http://test1.com/'> click </a> text text
other text <a class='links' href="gallery.html" title='Look at the gallery'> Gallery</a>
more text
Run Code Online (Sandbox Code Playgroud)

样本输出

text text text <a href='http://example.com/p.php?q=http://test1.com/'> click </a> text text
other text <a class='links' href="http://example.com/p.php?q=gallery.html" title='Look at the gallery'> Gallery</a>
more text
Run Code Online (Sandbox Code Playgroud)

我希望我已经说清楚了.无论如何,我正在尝试使用PHP和reg-ex.请你点赞我吧.

谢谢萨迪

php regex string preg-replace html-parsing

1
推荐指数
1
解决办法
1459
查看次数

替换和Aring; 用别的东西

如何&Aring;使用preg_replace 替换 其他字符串?谢谢!

php preg-replace special-characters

1
推荐指数
1
解决办法
921
查看次数

替换ereg_replace

可能重复:
如何在PHP中将ereg表达式转换为preg?

我升级了php,现在我得到了ereg_replace已弃用的错误.

我做了一些搜索网络,发现我可以使用preg但不知道如何正确更改此代码

$scriptName = ereg_replace(
    "^".$_SERVER["DOCUMENT_ROOT"], "", 
    $_SERVER["SCRIPT_FILENAME"]
);
Run Code Online (Sandbox Code Playgroud)

php regex preg-replace ereg-replace

1
推荐指数
1
解决办法
1307
查看次数

用php查找文本中的所有URL(链接)

我有这个代码正则表达式,它应该将所有类型的不同URL转换为某些文本中的链接.

preg_replace代码是:

$regex = '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';
$text = preg_replace($regex, '<a href="$1">$1</a>', $item);
Run Code Online (Sandbox Code Playgroud)

现在它适用于几乎所有你能想象到的URL,但我遇到的问题是逗号和URL中的特殊字符......

问题是让我:

http://www.sdfsdfsdf.sd/si/391,1000,1/more.html

http://sdfsddsdf-sdfsdfds.sr/component/option,com_contact/Itemid,3/lang,si/

有趣的是stackoverflow这两个都可以:)

谢谢,最好的问候,

php url text hyperlink preg-replace

1
推荐指数
1
解决办法
5344
查看次数

替换所有&与&amp; 只有不跟着放大器;

好吧,我在regexp中并不那么聪明,这就是我想做的事情

基本上,我有一个字符串与一些已经编码的实体,但我需要再次编码所有实体除了&amp;实体,所以如果我们有一个字符串

The Sun &amp; Mars are planets
Run Code Online (Sandbox Code Playgroud)

这将保持不变,因为我们不需要编码&&amp;再次

但是,如果我们有

The Sun &mdash; big hot planet
Run Code Online (Sandbox Code Playgroud)

这应该成为

The Sun &amp;mdash; big hot planet
Run Code Online (Sandbox Code Playgroud)

我知道这很愚蠢,但这就是一个解析器想要的东西.

php regex entities preg-replace

1
推荐指数
2
解决办法
4184
查看次数

preg_replace将电子邮件模板[[USER_FIRST_NAME]]添加到"John"

说我有这个HTML:

        <html>
            <head>
                <title>Hello [[USER_FIRST_NAME]]</title>
            </head>
            <body>
                <p>Hi, [[USER_FIRST_NAME]] [[USER_LAST_NAME]]!</p>
                <p><a href="mailto:[[USER_EMAIL]]">[[USER_EMAIL]]</a></p>
            </body>
        </html>
Run Code Online (Sandbox Code Playgroud)

我需要preg_replace所有[[FOOBAR]]带有价值观的代币.

最终结果:

        <html>
            <head>
                <title>Hello John</title>
            </head>
            <body>
                <p>Hi, John Smith!</p>
                <p><a href="mailto:john@smith.com">john@smith.com</a></p>
            </body>
        </html>
Run Code Online (Sandbox Code Playgroud)

php regex preg-replace

1
推荐指数
1
解决办法
2575
查看次数

PHP Regex - 从字符串中删除无效字符

我知道有很多正则表达式的问题,但我不太了解它,我也找不到足够好的解释来得出我想要得到的解决方案.

在PHP中,使用

preg_replace($pattern,"",$data); 
Run Code Online (Sandbox Code Playgroud)

我想允许所有字母数字字符以及减号,句号和下划线字符.我需要的$模式是什么?

php regex preg-replace

1
推荐指数
1
解决办法
4154
查看次数

PHP中preg_replace中的动态变量

我有一个这样的文本模板:

$template = [FIRSTNAME] say hello to [NAME]
Run Code Online (Sandbox Code Playgroud)

我有一个用户数组

[user_name] => myname
[user_firstname] => myfirstname
Run Code Online (Sandbox Code Playgroud)

我想改变它,preg_replace所以我尝试了这个:

$replacement = '$user[user_${1}]';
$result['texte'] = preg_replace('/[(.*)]/', {$replacement}, $result['texte']);
Run Code Online (Sandbox Code Playgroud)

没有任何成功:(

php regex preg-replace

1
推荐指数
1
解决办法
1485
查看次数

在php中处理数字输入失败

我有一个输入框,告诉我的用户将他们的Google Plus地址复制并粘贴到其中.这是输入框的代码:

<div class="row-fluid"><div class="span3 sidebar"><h4>Google Plus:</h4></div>
<div class="span9"><p> <input type="text" name="gplus" value="$data->googleplus" placeholder="Google Plus Profile URL">
</p></div></div><br/>
Run Code Online (Sandbox Code Playgroud)

这是处理输入的PHP代码:

$gplus=preg_replace("/[^0-9]/", "", $_POST['gplus']);
mysql_query( "UPDATE contacts SET googleplus='$gplus' WHERE username='$user'" )
or die ( "Could not update GooglePlus" );
Run Code Online (Sandbox Code Playgroud)

我只需要他们地址末尾的数字,因为其余的对每个人都是一样的.这是问题所在.当用户输入最多10位数时,它可以很好地存储数字.当用户输入超过10位数字时,它会将此数字存储在数据库中:"2147483647".始终是相同的数字.任何字母都应该被剥离,但是任何超过十位的数字都会导致这个数字出现.

我搜索了我的代码"2147483647",这不是任何一个!

php mysql preg-replace

1
推荐指数
1
解决办法
69
查看次数