[半吊子码农]wordpress 后台选项和 php 替换

2014-11-14 13:15:17 +08:00
 YAFEIML
之前是写在php文件里面,$replace3 = array('1','2','3');,每次更新比较麻烦都要改PHP文件
现在在WP后台整了个主题设置,整了2个字段
get_option('ed2k_replace3')
get_option('ed2k_replace4')

前台代码是这样的
$replace_i3 = get_option('ed2k_replace3');
$replace_i4 = get_option('ed2k_replace4');
$torrent_nameok = str_ireplace(stripslashes($replace_i3),stripslashes($replace_i4),substr_utf8($torrent_name,0,65));

那么问题来了,在后台怎样才能像PHP里面实现数组的效果比如array('1','2','3');

比如每一行一个词语,或者用,分割之类的
2628 次点击
所在节点    问与答
14 条回复
DearTanker
2014-11-14 13:41:39 +08:00
我也想知道这段关于PHP正则查询代码会出错的问题,该怎么办?

if ($s) {$title = preg_replace('/('.implode('|', $keys) .')/iu','<span style="color:#f40f40;text-decoration:underline;">\0</span>',$title);} ?>

具体错误是没办法找到“*”。会报错

Warning: preg_replace(): Compilation failed: nothing to repeat at offset
feiyuanqiu
2014-11-14 13:53:42 +08:00
@DearTanker *是一个正则符号 需要在前面加个反斜杠 \*
DearTanker
2014-11-14 15:16:46 +08:00
@feiyuanqiu 擦,认真看了一下,发现是我表达有问题

「具体错误是没办法找到“*”」的意思是,我在搜索框里面输入“*”号进行搜索会出错。

上面那段正则表达式的作用是关键词高亮。
abelyao
2014-11-14 15:18:22 +08:00
半吊子码农你好… 我每天都能看到你上来问几个问题,爱学习的孩子,要给你点个赞先
DearTanker
2014-11-14 15:20:31 +08:00
@feiyuanqiu 就像:
http://www.huging.com/?s=\*

http://www.huging.com/?s=*

请问、这段代码可以优化吗?

顺手贴一下代码:

<?php newtheme_switch('header.php') ?>
<div id="content">

<div class="goods<?php if(is_mobile()){echo ' ajax';}else{ ajax_ias();} ?>">

<?php if(have_posts()):query_posts($query_string .'&showposts='.newtheme_showposts());
echo '<p id="prompt">以下是为您搜索到的信息,如果没有您想要的,可尝试缩短或更换关键词。</p>';
while(have_posts()):the_post();
$title = get_the_title();
$keys = explode(" ",$s);
if ($s) {$title = preg_replace('/('.implode('|', $keys) .')/iu','<span style="color:#f40f40;text-decoration:underline;">\0</span>',$title);} ?>
<article>
<a class="thumbnail" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"<?php target_blank() ?>>
<?php newtheme_thumbnail() ?>
</a>

<?php if(newtheme_cn('links','link_url')):
if(newtheme_cn('goods','widget')): ?>
<span class="ali_625"><a data-type="0" biz-itemid="<?php ali_goods_id() ?>" data-tmplid="625" data-rd="2" data-style="2" data-border="0" href="#"></a></span>
<?php else : ?>
<a class="buy" href="<?php links_url() ?>" rel="nofollow noopener" target="_blank"><?php links_title() ?></a>
<?php endif;endif ?>

<h2>
<a href="<?php the_permalink() ?>" title="详细阅读:<?php the_title() ?>"<?php target_blank() ?>><?php echo $title ?></a><?php if(newtheme_cn('goods','subhead')):?><span class="subhead"><?php echo newtheme_cn('goods','subhead') ?></span><?php endif ?>
</h2>

<p class="info">
<time class="icon-time"><?php the_time( get_option('date_format') ) ?></time>
<span class="cat icon-star"><?php newtheme_ie('分类:');the_category('、') ?></span>
<?php newtheme_like('<span class="icon-like"></span>',' 喜欢') ?>
</p>

<p class="desc"><?php echo newtheme_excerpt(120) ?> <a href="<?php the_permalink() ?>" class="readmore" title="详细阅读:<?php the_title() ?>"<?php target_blank() ?>>查看全文</a></p>

</article>
<?php endwhile; else:

echo '<p id="prompt">非常抱歉,无法搜索到您可能需要的信息,我们为您随机推荐了以下文章。</p>';

$cat = get_option('cat_limit');
$showposts = newtheme_showposts();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'order' => DESC,
'orderby' => 'rand',
'cat' => $cat,
'post__not_in' => get_option('sticky_posts'),
'showposts' => $showposts,
'paged' => $paged
);
query_posts($args);
while (have_posts()) : the_post();
if(is_mobile()){
include('mobile/goods.php');
}else{
include('pc/goods.php');
}
endwhile;wp_reset_query();

endif;
?>

<?php newtheme_paging() ?>

</div>

</div>

<?php newtheme_switch('footer.php') ?>
feiyuanqiu
2014-11-14 15:25:07 +08:00
@DearTanker 我明白你的意思。
if ($s) {
$title = preg_replace('/('.implode('|', $keys) .')/iu','<span style="color:#f40f40;text-decoration:underline;">\0</span>',$title);
}
你这段代码的作用是 通过构造正则匹配式把$title中的搜索项$keys高亮出来
当你搜索*时,实际构造出来的正则是:'/key1|*|key2/iu' *在正则里面表示匹配0或多个,这里就出问题了
如果要搜索*,需要给*加反斜杠 \*,也就是最终构造出来的正则应该是:'/key1|\*|key2/iu' 这样才行。
DearTanker
2014-11-14 15:28:40 +08:00
@feiyuanqiu 对,= =关键是怎么改,我试着把\添加了几个地方发现都不行= =,PS:我不懂代码T.T
watsy0007
2014-11-14 15:34:32 +08:00
基本所有语言里面都关于字符串类型都会有1个函数。
split,专门用于拆解字符串为数组
msg7086
2014-11-14 15:52:09 +08:00
@DearTanker 你是把用户输入的内容直接当成正则来搜
那么必然用户输入的内容 *得是正则才行*。
要么自己写搜索算法,要么就把原始输入里是正则关键字的那些部分转义掉。典型的比如
. * + { } [ ] ( ) | ^ $ \
都是要转义的。
feiyuanqiu
2014-11-14 15:59:30 +08:00
@DearTanker 这个时候就该去招人了...

或者把这段代码加上去将就用下,没有仔细测试,可能有bug...
if ($s) {
// 下面这段是新添加的,放在这里就行
$keys = preg_replace('/(\\' . implode('|\\', array('\\', '^', '$', '*', '+', '?', '{', '}', ',', '?', '.', '(', ')', '[', ']', '-')) . ')/i', '\\\\$0', $keys);
// 原代码
$title = preg_replace('/('.implode('|', $keys) .')/iu','<span style="color:#f40f40;text-decoration:underline;">\0</span>',$title);
}
WildCat
2014-11-14 16:04:57 +08:00
unserialize()

serialize()
DearTanker
2014-11-14 16:07:21 +08:00
@feiyuanqiu 太感谢了,这段代码可以用,完美解决,以后只要出现错误的符号就在array里面加一个就好了,哈哈,真心感谢。。

这博客只是我平时抽点时间维护的,哪有“招人”的概念。哈哈。
DearTanker
2014-11-14 16:07:46 +08:00
@msg7086 还有你,感谢啦~
feiyuanqiu
2014-11-14 16:11:05 +08:00
....啊啊啊啊,上面我的代码作废
$title = preg_replace('/('.implode('|', array_map('preg_quote', $keys)) .')/iu','<span style="color:#f40f40;text-decoration:underline;">\0</span>',$title);
echo($title);

这样就行了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/146468

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX