我想在字符串和字符串处强制执行单个问号.在JavaScript中它完美地运作:
var re = /[?7!1]*$/;
document.write('lolwut'.replace(re, '?'));
document.write('lolwut?'.replace(re, '?'));
document.write('lolwut??'.replace(re, '?'));
document.write('lolwut???'.replace(re, '?'));
document.write('lolwut???!!!11'.replace(re, '?'));
Run Code Online (Sandbox Code Playgroud)
所有返回的值都等于"lolwut?" PHP变体不能顺利运行:
$re = '/[?7!1]*$/';
echo preg_replace($re, '?', 'lolwut') . "\n";
echo preg_replace($re, '?', 'lolwut?') . "\n";
echo preg_replace($re, '?', 'lolwut??') . "\n";
echo preg_replace($re, '?', 'lolwut???') . "\n";
echo preg_replace($re, '?', 'lolwut???!!!11') . "\n";
Run Code Online (Sandbox Code Playgroud)
输出是:
lolwut?
lolwut??
lolwut??
lolwut??
lolwut??
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
更新:
$(美元)断言字符串结尾
断言是对当前匹配点之后或之前的字符的测试,该字符实际上不消耗任何字符.
这是我的困惑,以及preg_replace的隐含全局标志,感谢salathe提供线索.(你们应该投票给他的答案,真的)
假设,我有MSWord文件source.doc,下一个内容是"Microsoft Word文件的内容".例如,我想通过PHP打开它并将单词"Microsoft"替换为"Openoffice"并将结果保存到result.doc中.以下代码使用preg_replace:
$content = file_get_contents( SOMEPATH . '/source.doc' );
$new_content = preg_replace( '/Microsoft/i', 'Openoffice', $content );
file_put_contents( SOMEPATH . '/target.doc', $new_content );
Run Code Online (Sandbox Code Playgroud)
或使用str_replace:
$content = file_get_contents( SOMEPATH . '/source.doc' );
$new_content = str_replace( 'Microsoft', 'Openoffice', $content );
file_put_contents( SOMEPATH . '/target.doc', $new_content );
Run Code Online (Sandbox Code Playgroud)
它们都不起作用.代码运行没有任何异常,但target.doc与source.doc相同.替换不执行.
我尝试了很多不同的接收器,比如正则表达式修改器,iconv等等,但没有任何帮助.
var_dump的$content示出的原始结构source.doc即充满不寻常的字符和作为我想一些它停止str_replace或preg_replace扫描.无法弄清楚它是哪个字符,如果我找到它我该怎么办.
var_dump的$new_content是相同的$内容.
感谢您的帮助!
我有以下PHP代码从变量中删除特殊字符;
<?php
$name = "my%^$@#name8";
$patterns = array( '/\s+/' => '_', '/&/' => 'and', '/[^[:alpha:]]+/' => '_');
$name2 = preg_replace(array_keys($patterns), array_values($patterns), trim($name));
echo $name2;
?>
Run Code Online (Sandbox Code Playgroud)
但是,除了特殊的字符,数字也会被下划线取代_.我想在结果中包含数字.我怎样才能解决这个问题?
尝试用PHP替换字符串($ content)中的height =""和width =""值,我试过preg替换无效,并建议我做错了什么?
示例内容如下:
$content = '<iframe width="560" height="315" src="http://www.youtube.com/embed/c0sL6_DNAy0" frameborder="0" allowfullscreen></iframe>';
Run Code Online (Sandbox Code Playgroud)
代码如下:
if($type === 'video'){
$s = $content;
preg_match_all('~(?|"([^"]+)"|(\S+))~', $s, $matches);
foreach($matches[1] as $match){
$newVal = $this->_parseIt($match);
preg_replace($match, $newVal, $s);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我只需要比赛并搜索我的身高和宽度
function _parseIt($match)
{
$height = "height";
$width = "width";
if(substr($match, 0, 5) === $height){
$pieces = explode("=", $match);
$pieces[1] = "\"175\"";
$new = implode("=", $pieces);
return $new;
}
if(substr($match, 0, 5) === $width){
$pieces = explode("=", $match);
$pieces[1] = "\"285\"";
$new = implode("=", $pieces);
return …Run Code Online (Sandbox Code Playgroud) 如何找到页面标题的名称$_SERVER['php_self']?让我们说$_SERVER我的页面显示如下:/application/mysite/signup.php.如何选择页面标题signup?
我得到以下字符串:
last_name, first_name
bjorge, philip
kardashian, [kghim]
mer#$##Code:menu:51587daa7030e##$#cury some more
data #$##Code:menu:515r4387daa7dsf030e##$#, freddie
Run Code Online (Sandbox Code Playgroud)
我试图用函数替换中间的代码:'codeParser'正则表达式是:
$PC_File = preg_replace_callback("(?=\#\$\#\#).*?(?<=\#\#\$\#)", 'codeParser', $PC_File);
Run Code Online (Sandbox Code Playgroud)
但得到这个错误:
PHP Warning: preg_replace_callback() : Unknown modifier '.'
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写评论表单.
我正在考虑使用strstr来查找是否<pre>存在以及是否确实使用htmlentities将其中的所有内容都转换为html.
但我遇到的问题是实际上在转换前的所有内容.
我的代码变为现实,但如何替换.
我正在看preg_replace但我的正则表达式真的很差:)
例:
$string = 'This is a form with <pre>Everything in this needs to be htmlentities() </pre> and everything outside needs to be normal.';
Run Code Online (Sandbox Code Playgroud) 我试图找出一种避免SQL注入的简单方法,到目前为止我只能提出两个想法:
这是我目前的代码:
<?php
$hash = $_GET['file'];
if (isset($hash))
{
$db = new SQLite3("Files.db");
if ($db != null)
{
$hash = preg_replace('/[^A-Za-z0-9 _.\-+=]/', '_', $hash);
if ($response = $db->query("SELECT [FILE] FROM '$hash'"))
{
echo $response->fetchArray()[0]; // File name is returned if successful.
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我的问题是我是以正确的方式进行这种方式还是有更好的方法来做到这一点?
我是Regex的新手.我有一个字符串:
Hello <b>ABCD</b> World
or
<b>ABCD</b>Hello World
Run Code Online (Sandbox Code Playgroud)
我基本上希望将文本保留在粗体标记内,但删除字符串中的所有其他字符.
我找到了删除字符串中粗体部分的代码:
$string = 'This is <b>an</b> example <b>text</b>';
echo preg_replace('/(<b>.+?)+(<\/b>)/i', '', $string);
Run Code Online (Sandbox Code Playgroud)
那么如何让它以相反的方式工作呢?
问候艾哈迈尔
我将有一个由HTML代码组成的字符串(一行),该字符串将存储在PHP变量中。该字符串来自HTML页面,该页面通常在标签之间包含换行符和空白。我们可以使用换行符(一个或多个)和/或空白,例如以下示例:
<h1>tag1</h>
<p>Between h ad p we have \s and \n</p>
Run Code Online (Sandbox Code Playgroud)
在执行正则表达式和preg_replace之后,我想要这个:
<h1>tag1</h><p>Between h ad p we have \s and \n</p>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过此正则表达式,但不是workig。
$str=<<<EOF
<h1>tag1</h>
<p>Between h ad p we have \s and \n</p>
EOF;
$string = trim(preg_replace('/(>\s+<)|(>\n+<)/', ' ', $str));
Run Code Online (Sandbox Code Playgroud)
在这里您可以找到完整的代码http://www.phpliveregex.com/p/7Pn
php ×10
preg-replace ×10
regex ×8
ms-word ×1
php-5.3 ×1
preg-match ×1
replace ×1
sqlite ×1
str-replace ×1
string ×1