我有一种奇怪的事情,我真的需要我的文本格式化.不要问我为什么我做了这个奇怪的事情!;-)
所以,我的PHP脚本用一个像"|"这样的特殊符号替换所有行折叠"\n".当我将文本数据插入数据库时,PHP脚本用符号"|"替换所有行折叠 当脚本从数据库中读取文本数据时,它将替换所有特殊符号"|" 用折叠线"\n".
如果在每个分隔文本中使用超过2行折叠,我想限制文本格式以剪切行折叠.
以下是我希望脚本格式化的文本示例:
this is text... this is text... this is text...this is text...this is text... this is text... this is text... this is text... this is text... this is text...
this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text... this is text...
Run Code Online (Sandbox Code Playgroud)
我想重写格式,如:
this is text... this is text... this is text...this is text...this is text... this is …Run Code Online (Sandbox Code Playgroud) 我很难做一个简单的事情str_replace.
我试图从字符串中删除所有逗号,这确实有效.但是当我再次尝试改变字符串时,逗号再次以某种方式重新出现.我重构了我的代码以确保我没有引起这个.
无论如何这里是我的代码.任何人都可以发现一个错误吗?
$delim=remDelim(fgets($fo));
# echo 'before : '.htmlspecialchars($delim);
$delime =str_replace(",",",",$delim);
echo 'after : '.htmlspecialchars($delime);
$delimed = str_replace("<","",$delime);
echo $delimed.'<br />';
example output:
Jose, jr to Jose, jr to Jose, jr
Run Code Online (Sandbox Code Playgroud)
它就像我的字符串不知何故未被转换.我会说,我很可能把名字混淆了,并用第一个字符串来制作最后一个.但唉,我可以看到情况并非如此.
好吧,我会更容易添加表情符号和应该更容易更换的代码,因此创建2个数组,我想只有一个.所以这是我想要的一个例子.$ Smileys = array(":D"=>"");
所以不要像$ Smileys = array(":D")那样使用它; $ SmileyReplace = array(""); 但它有可能吗?我似乎无法在Google上找到有用的东西.
我想要使用的新代码和旧代码.新:
function fixSmileys($Data) {
$Smileys = array(
":D" => '<img src="/application/modules/Chat/externals/images/smilies/grin.png" title=":D" alt=":D"/>',
":)" => '<img src="/application/modules/Chat/externals/images/smilies/smile.png" title=":)" alt=":)"/>',
":P" => '<img src="/application/modules/Chat/externals/images/smilies/tongue.png" title=":P" alt=":P"/>',
":S" => '<img src="/application/modules/Chat/externals/images/smilies/confused.png" title=":S" alt=":S"/>',
":'(" => '<img src="/application/modules/Chat/externals/images/smilies/cry.png" title=":'."'".'(" alt=":Cry:"/>',
":$" => '<img src="/application/modules/Chat/externals/images/smilies/embarrassed.png" title=":$" alt=":$"/>',
":(" => '<img src="/application/modules/Chat/externals/images/smilies/frown.png" title=":(" alt=":("/>',
":@" => '<img src="/application/modules/Chat/externals/images/smilies/mad.png" title=":@" alt=":@"/>',
";)" => '<img src="/application/modules/Chat/externals/images/smilies/wink.png" title=";)" alt=";)"/>',
"B)" => '<img src="/application/modules/Chat/externals/images/smilies/cool.png" title="B)" alt="B)"/>', …Run Code Online (Sandbox Code Playgroud) 我有一个网站需要向用户显示电话号码,而不是显示某人从源代码中获取的实际数字,我想用相应的单词替换电话号码的每个数字.例如...
355-758-0384
Would become
three five five - seven five eight - zero three eight four
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?对不起,我没有任何代码要显示,因为我甚至不知道从哪里开始.
我有一系列颜色存储,如此呈现.如您所见,color_codes存储有哈希和逗号.
Array
(
[0] => Array
(
[0] => Array
(
[item_color] => Black
[color_codes] => #000000,#000000,
)
[1] => Array
(
[item_color] => Red
[color_codes] => #FF0033,
)
)
[1] => Array
(
[0] => Array
(
[item_color] => White
[color_codes] => #FFFFFF,
)
[1] => Array
(
[item_color] => Black
[color_codes] => #0C0C0C,#0C0C0C,
)
)
)
Run Code Online (Sandbox Code Playgroud)
是否可以使用explode删除每种颜色前面的#并用 - (破折号)符号替换逗号.
foreach通过每个项目实现这一目标吗?我已经尝试删除哈希,所以把我没有运气.
for ($i = 0; $i < count($colours); $i++) {
$colours[$i]['color_codes'] = str_replace('#', '', $colours[$i]['color_codes']);
}
Run Code Online (Sandbox Code Playgroud)
我的目标是,如果有两个例如,颜色看起来像这样 FFFFFF-FFFFFF-
我在这里得到了这个代码:
<?php
include_once("php_includes/loginLinks.php");
str_replace("member_profile","php_includes/member_profile",$loginLinks);
str_replace("member_account","php_includes/member_account",$loginLinks);
str_replace("logout","php_includes/logout",$loginLinks);
str_replace("register_form","php_includes/register_form",$loginLinks);
str_replace("login","php_includes/login",$loginLinks);
echo $loginLinks;
?>
Run Code Online (Sandbox Code Playgroud)
它应该改变链接的根文件夹,我很确定那里存在一些逻辑错误.
问题: 如何将新回显的链接目录的链接目录更改为另一个目录,或者是另一种更改它的方式(我在PHP中很新,所以不是很擅长)?
所有!
新手在这里!我试图创建一个允许我替换四个字符(ACTG)的函数.为了更简洁,我想用'T'代替'A',用'G'代替'C',反之亦然.
我到目前为止的代码是这样的,但我得到一个错误(翻译预期至少有1个参数,得到0).
#!/usr/bin/python
from sys import argv
from os.path import exists
def translate():
str.replace("A", "T");
str.replace("C", "G");
str.replace("G", "C");
str.replace("T", "A");
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
in_file = open(from_file)
indata = in_file.read()
newdata = indata.translate()
out_file = open(to_file, 'w')
out_file.write(newdata[::-1])
out_file.close()
in_file.close()
Run Code Online (Sandbox Code Playgroud)
我尝试给translate函数一个argument(def translate(str))并用str(newdata = indata.translate(str))调用它,但那些也没用.
将不胜感激任何帮助和指导.
我试图在字符串中找到所有大写字母,并用小写加underscore字符替换它.AFAIK没有标准的字符串函数来实现这个(?)
例如,如果输入字符串是'OneWorldIsNotEnoughToLive'输出字符串应该是'_one_world_is_not_enough_to_live'
我能用以下代码完成:
# This finds all the uppercase occurrences and split into a list
import re
split_caps = re.findall('[A-Z][^A-Z]*', name)
fmt_name = ''
for w in split_caps:
fmt_name += '_' + w # combine the entries with underscore
fmt_name = fmt_name.lower() # Now change to lowercase
print (fmt_name)
Run Code Online (Sandbox Code Playgroud)
我觉得这太过分了.首先re,然后是列表迭代,最后转换为小写.也许有一种更简单的方法来实现这一点,更多的pythonic和1-2行.
请建议更好的解决方案 谢谢.
我知道这个主题的变体已经在别处讨论过,但其他线程都没有帮助.
我想将一个字符串从python移交给sql.然而,可能会发生撇号(')出现在字符串中.我想用反斜杠逃脱它们.
sql = "update tf_data set authors=\'"+(', '.join(authors).replace("\'","\\\'"))+"\' where tf_data_id="+str(tf_data_id)+";"
Run Code Online (Sandbox Code Playgroud)
但是,这总是会\\'在我的字符串中给出.因此,反斜杠本身被转义,并且sql语句不起作用.
有人可以帮助我,或者让我替代我这样做的方式吗?谢谢
我正在尝试实现我以前在Excel中执行的功能,但无法找到实现它的方法.
我有两个数据集:一个是我的基础数据集,另一个是查找表.我的基地有两列,人的名字和姓氏.我的查找表也包含前两列,但它还包含替换的名字.
People <- data.frame(
Fname = c("Tom","Tom","Jerry","Ben","Rod","John","Perry","Rod"),
Sname = c("Harper","Kingston","Ribery","Ghazali","Baker","Falcon","Jefferson","Lombardy")
)
Lookup <- data.frame(
Fname = c("Tom","Tom","Rod","Rod"),
Sname = c("Harper","Kingston","Baker","Lombardy"),
NewFname = c("Tommy","Tim","Roderick","Robert")
)
Run Code Online (Sandbox Code Playgroud)
我想要做的是用NewFname替换Fname,这取决于两个条件:两个数据帧中的Fname和Sname匹配.这是因为我有一个数据集,其中包含需要处理的其他40,000行数据.最后,我希望最终得到以下数据框:
People <- data.frame(
Fname = c("Tommy","Tim","Jerry","Ben","Roderick","John","Perry","Robert"),
Sname = c("Harper","Kingston","Ribery","Ghazali","Baker","Falcon","Jefferson","Lombardy")
)
Run Code Online (Sandbox Code Playgroud)
但是,我想要一个功能解决方案,所以我不必单独手动输入条件和替换名称.到目前为止,我有以下(有问题的)解决方案,这将涉及在dplyr中使用mutate生成一个新列,但它不起作用
People %>%
mutate(NewName = if_else(
Fname == Lookup$Fname & Sname == Lookup$Sname, NewFname, Fname
))
Run Code Online (Sandbox Code Playgroud) str-replace ×10
php ×6
python ×3
dplyr ×1
explode ×1
folding ×1
formatting ×1
function ×1
line ×1
preg-replace ×1
pymysql ×1
r ×1
regex ×1
string ×1