我试图模仿json_encodePHP 5.3.0中实现的位掩码标志,这是我的字符串:
$s = addslashes('O\'Rei"lly'); // O\'Rei\"lly
Run Code Online (Sandbox Code Playgroud)
做json_encode($s, JSON_HEX_APOS | JSON_HEX_QUOT)输出如下:
"O\\\u0027Rei\\\u0022lly"
Run Code Online (Sandbox Code Playgroud)
我目前正在使用早于5.3.0的PHP版本:
str_replace(array('\\"', "\\'"), array('\\u0022', '\\\u0027'), json_encode($s))
or
str_replace(array('\\"', '\\\''), array('\\u0022', '\\\u0027'), json_encode($s))
Run Code Online (Sandbox Code Playgroud)
哪个正确输出相同的结果:
"O\\\u0027Rei\\\u0022lly"
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么我需要更换单引号('\\\''甚至是"\\'"[ 不包括周围的引号 ])'\\\u0027'而不仅仅是'\\u0027'.
这是我在移植到PHP <5.3时遇到问题的代码:
if (get_magic_quotes_gpc() && version_compare(PHP_VERSION, '6.0.0', '<'))
{
/* JSON_HEX_APOS and JSON_HEX_QUOT are availiable */
if (version_compare(PHP_VERSION, '5.3.0', '>=') === true)
{
$_GET = json_encode($_GET, JSON_HEX_APOS | JSON_HEX_QUOT);
$_POST = json_encode($_POST, JSON_HEX_APOS | JSON_HEX_QUOT);
$_COOKIE = json_encode($_COOKIE, …Run Code Online (Sandbox Code Playgroud) 以下是我正在使用的PHP代码
$file_handle = fopen("products.csv", "r");
$fname = "products.csv";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$server = "**\******,1433";
$connectionInfo = array( "Database"=>"******", "UID"=>"***", "PWD"=>"*******" );
$conn = sqlsrv_connect( $server, $connectionInfo );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$itemco = $line_of_text[0];
$sql = "SELECT quantity FROM Item WHERE itemlookupcode = '$itemco' ";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} …Run Code Online (Sandbox Code Playgroud) 我想制作一个PHP函数,它可以在双星号之间加粗,而在一个星号之间使用斜体(非常类似于stackoverflow上的编辑器).
同样的规则适用,如果*和单词之间有空格,则不应呈现.
谁能帮助我?我试过,但我只走到这一步,因为我不知道如何制作奇怪的星号"<b>"和偶数的"</ b>".
(我不能在没有空格的情况下键入它们,stackoverflow会将文本呈现为粗体.....)
$thenewtext = str_replace("**", "<b>", "**Hello World** of PHP");
Run Code Online (Sandbox Code Playgroud) 我有缅甸语的文字,UTF-8.我正在使用PHP来处理文本.在此过程中的某些时候,一些ZWSP已经悄悄进入,我想将它们删除.我尝试了两种不同的方法来删除字符,似乎都没有用.
首先我尝试使用:
$newBody = str_replace("​", "", $newBody);
Run Code Online (Sandbox Code Playgroud)
搜索HTML实体并将其删除,因为这是它在Web Inspector下的显示方式.空格不会被删除.我也尝试过:
$newBody = str_replace("​", "", $newBody);
Run Code Online (Sandbox Code Playgroud)
得到相同没有结果.
我尝试的第二种方法是在这个问题中找到从PHP中的字符串中删除ZERO WIDTH NON-JOINER字符
看起来像这样:
$newBody = str_replace("\xE2\x80\x8C", "", $newBody);
Run Code Online (Sandbox Code Playgroud)
但我也没有结果.ZWSP未被删除.
An example word in the text ($newBody) looks like this : ???​??​????
And I want to make it look like this : ????????
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?preg_replace会以某种方式更好地工作吗?
所以我试过了
$newBody = preg_replace("/\xE2\x80\x8B/", "", $newBody);
Run Code Online (Sandbox Code Playgroud)
它似乎是运作,但现在还有另一个问题.
<a class="defined" title="Ukraine">??​?​?????</a>
Run Code Online (Sandbox Code Playgroud)
变成了
<a class="defined _tt_t_" title="Ukraine" style="font-family: 'Masterpiece Uni Sans', TharLon, Myanmar3, Yunghkio, Padauk, Parabaik, 'WinUni Innwa', 'Win Uni Innwa', 'MyMyanmar …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的字符串:
something-------another--thing
//^^^^^^^ ^^
Run Code Online (Sandbox Code Playgroud)
我想用一个破折号替换多个破折号.
所以预期的产量是:
something-another-thing
//^ ^
Run Code Online (Sandbox Code Playgroud)
我尝试使用str_replace(),但我必须为每一个可能的破折号再次编写代码.那么如何用一个短划线替换任何数量的破折号呢?
对于Rizier:
尝试:
$mystring = "something-------another--thing";
str_replace("--", "-", $mystring);
str_replace("---", "-", $mystring);
str_replace("----", "-", $mystring);
str_replace("-----", "-", $mystring);
str_replace("------", "-", $mystring);
str_replace("-------", "-", $mystring);
str_replace("--------", "-", $mystring);
str_replace("---------", "-", $mystring);
etc...
Run Code Online (Sandbox Code Playgroud)
但是这个字符串在两个单词之间可能有10000行.
String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i++;", "");
Run Code Online (Sandbox Code Playgroud)
//期望的输出:: newCode = "helloworld";
但这并不是用空白取代i ++.
我有一个包含£符号的HTML字符串,由于某种原因我无法替换它们.我假设这是一个编码问题,虽然我无法弄清楚如何.该网站使用ISO-8859-1进行编码
$str = '<span class="price">£89.99</span>';
var_dump(mb_detect_encoding($str, 'ISO-8859-1', true)); // outputs ISO-8859-1
echo str_replace(array("£","£"),"",$str); // nothing is removed
echo htmlentities($str); // the entire string is converted, including £ to £
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑
本来应该指出我要用£代替£ - 我已暂时添加£到要替换的项目数组中,以防它已被转换
假设我有一个长字符串:pneumonoultramicroscopicsilicovolcanoconiosis。我想stringr::str_replace_all用其他字母替换某些字母。根据文档,str_replace_all可以采用命名向量并用值替换名称。这适用于 1 次替换,但对于多次替换,它似乎是迭代进行的,因此结果是对上一次迭代的替换。我不确定这是预期的行为。
library(tidyverse)
text_string = "developer"
text_string %>%
str_replace_all(c(e ="X")) #this works fine
[1] "dXvXlopXr"
text_string %>%
str_replace_all(c(e ="p", p = "e")) #not intended behaviour
[1] "develoeer"
Run Code Online (Sandbox Code Playgroud)
想要的结果:
[1] "dpvploepr"
Run Code Online (Sandbox Code Playgroud)
我通过引入一个新角色得到:
text_string %>%
str_replace_all(c(e ="X", p = "e", X = "p"))
Run Code Online (Sandbox Code Playgroud)
这是一个可用的解决方法,但很难推广。这是一个错误还是我的期望错误?
我还希望能够同时用n 个其他字母替换n个字母,最好使用两个向量(如“旧”和“新”)或命名向量作为输入。
reprex 已编辑以便于人类阅读
我正在尝试从在此链接中找到的 .txt 文件构建一个语料库。我相信 的实例\xad应该是“软连字符”,但在 UTF-8 编码下似乎无法正确读取。我尝试iso8859-15使用以下代码将 .txt 文件编码为:
with open('Harry Potter 3 - The Prisoner Of Azkaban.txt', 'r',
encoding='iso8859-15') as myfile:
data=myfile.read().replace('\n', '')
data2 = data.split(' ')
Run Code Online (Sandbox Code Playgroud)
这将返回一个 'words' 数组,但 '\xad' 仍然附加到 data2 中的许多条目。我试过了
data_clean = data.replace('\\xad', '')
Run Code Online (Sandbox Code Playgroud)
和
data_clean = data.replace('\\xad|\\xad\\xad','')
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有删除 '\xad' 的实例。有没有人遇到过类似的问题?理想情况下,我想将此数据编码为 UTF-8 以使用该nltk库,但它不会读取具有 UTF-8 编码的文件,因为我收到以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 471: invalid start byte
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
附加上下文:这是一个娱乐项目,旨在能够基于 txt 文件生成故事。到目前为止,我生成的所有内容都充满了 '\xad',这破坏了乐趣!
我尝试用另一列中的值替换字符串时遇到替换问题。我想用 df['Length'] 替换 'Length'。
df["Length"]= df["Length"].replace('Length', df['Length'], regex = True)
Run Code Online (Sandbox Code Playgroud)
下面是我的数据
Input:
**Formula** **Length**
Length 5
Length+1.5 6
Length-2.5 5
Length 4
5 5
Expected Output:
**Formula** **Length**
5 5
6+1.5 6
5-2.5 5
4 4
5 5
Run Code Online (Sandbox Code Playgroud)
但是,使用我上面使用的代码,它将替换我的整个单元格,而不是仅替换长度。我得到低于输出:我发现这是由于使用了 df['column'],如果我使用任何其他字符串,后面的偏移量(-1.5)将不会被替换。
**Formula** **Length**
5 5
6 6
5 5
4 4
5 5
Run Code Online (Sandbox Code Playgroud)
我可以知道其他列的值是否有任何替换方法?
谢谢你。
str-replace ×10
php ×6
encoding ×2
python ×2
string ×2
csv ×1
escaping ×1
java ×1
json ×1
pandas ×1
r ×1
replaceall ×1
sql-server ×1
stringr ×1
unicode ×1