[英]PHP preg_replace
I'm stuck with this one... 我被这个卡住了...
I insert some data into a database table which comes from a form. 我将一些数据插入来自表单的数据库表中。 Sometimes, this data contains html, so I do
有时,这些数据包含html,所以我这样做
$note = htmlentities(mysqli_real_escape_string($this->db, $_POST['note']));
I then store the $note in my database. 然后,我将$ note存储在数据库中。 When retrieving my notes, I check whether there is html code in it, and if there is, I want to have it highlighted using
highlight_string()
. 检索笔记时,我检查其中是否有html代码,如果有,我想使用
highlight_string()
突出highlight_string()
它。 That part works like a charm. 那部分就像一个魅力。 Now here's the problem:
现在这是问题所在:
I am displaying the highlighted string in a div container, but it's overflowing because the white spaces are being converted to
我正在div容器中显示突出显示的字符串,但是由于空白将被转换为
而导致溢出 . 。 I'm trying to remove those non-breaking spaces and replace them with a simple space so the highlighted string will be contained in the div.
我试图删除那些不间断的空格,并用一个简单的空格替换它们,以便突出显示的字符串将包含在div中。 I have tried this so far:
到目前为止,我已经尝试过了:
$note = html_entity_decode($note);
$note = highlight_string($note);
$note = preg_replace('/ /', '', $note);
I have also tried $note = str_replace(' ', ' ', $note);
我也尝试过
$note = str_replace(' ', ' ', $note);
, to no avail. ,无济于事。
Any help is very much appreciated! 很感谢任何形式的帮助! Thanks :)
谢谢 :)
I think the problem is quite simply that highlight_string()
is outputting its result immediately, rather than saving it to $note
. 我认为问题很简单,
highlight_string()
立即输出其结果,而不是将其保存到$note
。
Instead, please try the following: 相反,请尝试以下操作:
$note = html_entity_decode($note);
$note = highlight_string($note, true);
$note = str_replace(' ', ' ', $note);
The difference in my code is that I use highlight_string($note, true)
with the second parameter set to true. 我的代码的不同之处在于,我使用了
highlight_string($note, true)
并将第二个参数设置为true。 The docs shed some light about the function's behavior: 该文档对函数的行为进行了说明:
mixed highlight_string ( string $str [, bool $return = false ] )
混合highlight_string(字符串$ str [, bool $ return = false ])
Return
返回
Set this parameter to TRUE to make this function return the highlighted code.将此参数设置为TRUE可使此函数返回突出显示的代码。
The regex function you have in your code block might work, but since this is a simple replacement, it will suffice to use str_replace
in this case, as you have tried. 您在代码块中具有的regex函数可能会起作用,但是由于这是一个简单的替换,因此您可以尝试使用
str_replace
在这种情况下就足够了。
You can try with strip_tags in place of htmlentities 您可以尝试使用strip_tags代替htmlentities
string strip_tags( string $str [, string $allowable_tags ] )
In allowable_tags, you can define the html you want to allow in string. 在allowable_tags中,您可以在字符串中定义要允许的html。 If null it will not allow any html to get apply.
如果为null,则不允许应用任何html。
Reference : http://in3.php.net/manual/en/function.strip-tags.php 参考: http : //in3.php.net/manual/en/function.strip-tags.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.