English 中文(简体)
PHP, 爆炸多个单词并用字符前缀 [重复]
原标题:PHP, explode multiple words and prefix them with a character [duplicate]
  • 时间:2012-05-23 07:10:59
  •  标签:
  • php
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Add a prefix to each item of a PHP array

我尝试使用布尔模式 全文搜索文本 。

如果用户类型超过两个单词, 则会做出这样的拼写 。

Search Keyword :   Apple Macbook Air 

Result :   +Apple +Macbook +Air

所以,我做了个php命令 来让这一切成真,但是没有成功。

$ArrayKeywords = explode(" ", $b_keyword);
for($i = 0; $i < sizeof($ArrayKeywords); $i++)
$array_keyword =  +"$ArrayKeywords[$i]"  ;

你能告诉我怎么做吗?

最佳回答

也可以 (< a href=>""http://codepad.viper-7.com/grS1u1] rel = “ no follow” >demo )

echo preg_replace( (w+) ,  +$0 ,  Apple Macbook Air );
问题回答

为什么不干脆这样做:

$keyword =  +  . trim(str_replace(   ,  + ,$b_keyword));

在那里不需要真正 环路 < code> 的 < code>

// trims off consecutive spaces in the search term
$b_keyword = preg_replace( /s+/ ,    , $b_keyword); 
$ArrayKeywords = explode(" ", $b_keyword);
echo  +  . implode(  + , $ArrayKeywords);

我看到有点误会 里面的变数是如何运作的

你可能已经知道 单引号中的任何内容 都会像往常一样被解析 在双引内 变量内容被打印

$foo =  bar ;

echo  foo: $foo ;    // result:  foo: $foo
echo "foo: $foo";    // result:  foo: bar

但是,您无法将两种方法结合起来; 在单引号 上加上双引号 。 单引号 并不能打印变量内容。 只有在“ em> entire 字符串被它们限定的情况下, 双引号才会起作用。 因此, 以下的参数不会起作用 :

echo  foo: "$foo" ;  // result:  foo: "$foo"

把这个扩展至你的情况, 你可以用双引号取代单引号, 然后把内部的双引号掉下来。

$array_keyword .= "+$ArrayKeywords[$i] ";

请注意,您必须将新单词混入变量(. /code>),否则该变量在每个循环上被覆盖。

旁注:在通过数组循环时,使用 foreach 环比使用 for 环更容易得多:

$ArrayKeywords = explode(" ", $b_keyword);
$array_keyword =   ;
foreach( $ArrayKeywords as $keyword ) {
    $array_keyword .=  +$keyword ";
}

如果您想要用于标签、新线、空格等 。

echo preg_replace( (s+) ,  $0+ ,   Apple Macbook Air );
//output: +Apple +Macbook +Air




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...