PHP将数据库内容分解为单个关键字?

时间:2015-12-17 03:03:57

标签: php

我正在构建关键字搜索的建议框,目前我正在接收文章的全部内容(标题,作者,标签,类别,文章内容) 这将是:

 Likable Guy at the Party SSCOTT CHRISTIAN Don't try to be Johnny Depp at the Oscars; just stick to a tux, with a tuxedo shirt, black bow tie, trousers with a satin stripe and, preferably, patent leather shoes. (If it's good enough for 007, it's good enough for you.) Fashion style runway

在编码成json以进入实时前端之前,你如何打破单个单词以使它看起来像这样:

Likable, Guy, at, the, Party, SSCOTT, CHRISTIAN, Don't, try, to, be, Johnny, Depp.......

2 个答案:

答案 0 :(得分:1)

您可以使用explode(' ', $sentence)然后implode添加逗号,但是标点符号会产生一些不良后果:

$s = implode(', ', explode(' ', $s));

将输出:

  

Likable,Guy,at,Party,SSCOTT,CHRISTIAN,Do not,try,to,be,   Johnny,Depp,at,Oscars ;, just,stick,to,a,tux ,, with,a,   燕尾服,衬衫,黑色,蝴蝶结,领带,裤子,有,a,缎,条纹,   而且,最好是专利,皮革,鞋子,(如果,它是好的,足够的,   对于,007,它是,好的,足够的,因为,你。),时尚,风格,跑道

另一个选择是先删除标点符号。您可以使用preg_replace执行此操作以仅保留字母和撇号:

$s = preg_replace("/[^a-z']+/i", ' ', $s); 
$s = implode(', ', explode(' ', $s));

将输出:

  

Likable,Guy,at,Party,SSCOTT,CHRISTIAN,Do not,try,to,be,   Johnny,Depp,at,the Oscars,just,stick,to,a,tux,with,a,   燕尾服,衬衫,黑色,蝴蝶结,领带,裤子,用,a,缎子,条纹,和   优选,专利,皮革,鞋子,如果,它是,好的,足够的,因为,它,   好的,足够的,你,时尚,风格,跑道

答案 1 :(得分:0)

$dest_image = imagecreatetruecolor($percent, $percent);

imagealphablending( $dest_image, false );
imagesavealpha( $dest_image, true );
$transparent = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
imagefill($dest_image, 0, 0, $transparent);


imagecopyresampled($dest_image, $overlay_gd_image, 0, 0, 0, 0, $percent, $percent, $overlay_width, $overlay_height);

imagecopymerge($source_gd_image, $dest_image, round($positionx), round($positiony), 0, 0, $percent, $percent, WATERMARK_OVERLAY_OPACITY);

imagejpeg($source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY);
imagedestroy($source_gd_image);