English 中文(简体)
以单个空格替换的非字母数字字符
原标题:Non alphanumeric characters replaced with a single space
  • 时间:2012-05-23 15:58:21
  •  标签:
  • php

在这里,我试图得到一个搜索引擎 取名字输入 Doe J 而不是 Doe, J。

这是我的代码:

$sql_where = array();

if (isset($_GET[ name ])) {

    echo "Searched: {$_GET[ name ]}<br>";

    $names = explode(   , trim(preg_replace( / +/ ,    , $_GET[ name ])));
    $names_cnt = count($names);

    if (2 == $names_cnt) {

        foreach ($names as $name_idx => $name) {

            if (($name_idx+1) == $names_cnt) {
                // last one
                $sql_where[] = "
                    (full_name like  % {$name}% )
                    ";
            } else {
                // first one
                $sql_where[] = "
                    (full_name like  {$name}% )
                    ";
            }
        }
    } else {

        $sql_where[] = "
            (full_name like  " . $DB->cleanString($_GET[ name ]) . "% )
            ";

我尝试了 /[a-zA-Z0-9]/ 和其他一些未成功修改的改动 。

问题回答
echo preg_replace( "`[^a-zA-Z0-9]+`", " ", $string); 
//replaces all non alpha numeric characters as " " 
//(and will not have duplicate spaces)

DEMO:“http://codepad.org/7Ltx3kcr' rel=“nofollow'>http://codepad.org/7Ltx3kcr





相关问题
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 ...

热门标签