特殊字符/数字的搜索功能

时间:2013-08-20 04:38:52

标签: php arrays loops

Hello ive为abc chars做了一个循环

但现在的问题是,#不要选择所有带有特殊字符/数字的标题。

<?PHP
$page_nav = array();
$r = 35;
$rr = chr($r);
for($i = 97; $i <= 122; ++$i )
{ 
    $l = chr($i);
    $page_nav[] = ($l == mysql_real_escape_string($_GET['order'])) ? "<li class='aktiv'>".$l."</li>" : "<li><a href='index.php?s=all-movies&order=".$l."&site=1'>".$l."</a></li>"; 
} 
echo implode(' ', $page_nav);
echo ($rr == mysql_real_escape_string($_GET['order'])) ? "<li class='aktiv'>".$rr."</li>" : "<li><a href='index.php?s=all-movies&order=".$rr."&site=1'>".$rr."</a></li>"; 
?>

我的查询:

$sqlCmdSearch="SELECT id,name,title,countryyear,type,hoster,cover,picturequality,language,rating,date FROM topmovies.movies WHERE status='online' AND type='movie' AND title LIKE '".mysql_real_escape_string($_GET['order'])."%' GROUP BY title ORDER BY title";

2 个答案:

答案 0 :(得分:1)

就像我在评论中所说,index.php?s=all-movies&order=#&site=1等于index.php?s=all-movies&order=

尝试urlencode,如果$order#,请从查询中删除title LIKE表达式
要使用特殊字符获取所有title,请使用REGEXP表达式

请参阅this answer

<?php
  $order    = ( isset( $_GET["order"] ) ) ? strtolower( mysql_real_escape_string( trim( $_GET["order"] ) ) ) : "#";
  $order    = ( !$order ) ? chr( 35 ) : $order;

  $page_nav = array();
  $chars    = array_merge( range( "a", "z" ), array( chr( 35 ) ) );

  foreach( $chars as $chr ) {
    $page_nav[] = ( $chr === $order ) ? "<li class='aktiv'>".$chr."</li>" : "<li><a href='index.php?s=all-movies&order=".urlencode( $chr )."&site=1'>".$chr."</a></li>"; 
  }
  echo implode( "\n", $page_nav );

  $sqlCmdSearch = "
    SELECT `id`, `name`, `title`, `countryyear`, `type`, `hoster`, `cover`, `picturequality`, `language`, `rating`, `date`
    FROM `topmovies`.`movies`
    WHERE
      `status` = 'online'
      AND
      `type` = 'movie'
      AND
      %s
    GROUP BY `title`
    ORDER BY `title`
  ";

  if ( $order === chr( 35 ) ) {
    // This query will return all rows where title contains any non-alphanumeric characters.
    $sqlCmdSearch = sprintf( $sqlCmdSearch, "NOT `title` REGEXP '[A-Za-z]'" );
  }
  else {
    $sqlCmdSearch = sprintf( $sqlCmdSearch, "`title` LIKE '%{$order}%'" );
  }

  // now do your db query here
?>

答案 1 :(得分:0)

通过循环查看字符,它无法访问特殊字符。这是因为特殊字符在utf-8表上有更高的数字。前255个与ASCII表完全相同,并且不知道任何特殊字符(例如,没有ö/ä/ ...)
一个解决方案是你创建一个数组并将所有你想要循环的字符包含在其中,然后在该数组中创建一个foreach。