Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:46:53+00:00 2026-06-07T10:46:53+00:00

I have the following PHP search script. When I search for 1234567 , it

  • 0

I have the following PHP search script.
When I search for 1234567, it matches the exact phrase but I want it to match only initial 4 characters i.e. 1234.

Explanation:

I want it to count the initial 4 characters and display the result matching those initial characters. E.g. if someone search for 1234567 then the script should count the initial 4 characters i.e. 1234 and show the results. Similarly if someone search 456789 then the script should count the initial 4 characters i.e. 4567 and show the results.

///explode search term
           $search_exploded = explode(" ",$search);
           foreach($search_exploded as  $search_each)
           {
           //construct query

            $x++;
            if ($x==1)
               $construct .= "keywords LIKE '%$search_each%'";
           else
              $construct .= " OR keywords LIKE '%$search_each%'";

           }


     //echo out construct

    $construct = "SELECT * FROM numbers WHERE $construct";
     $run = mysql_query($construct);

     $foundnum = mysql_num_rows($run);

     if ($foundnum==0)
        echo "No results found.";
     {

       echo "$foundnum results found.<p><hr size='1'>";

       while ($runrows = mysql_fetch_assoc($run))
       {
        //get data
        $title = $runrows['title']; 
        $desc = $runrows['description'];
       $url = $runrows['url'];

        echo "<b>$title</b>
             <b>$desc</b>
       <a href='$url'>$url</a><p>";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:46:55+00:00Added an answer on June 7, 2026 at 10:46 am

    You can replace your foreach with:

    foreach($search_exploded as $search_each)
    {
        $str = mysql_real_escape_string(substr($search_each, 0, 4));
        //construct query
        $x++;
        if ($x==1) $construct .= "keywords LIKE '$str%'";
        else       $construct .= " OR keywords LIKE '$str%'";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made the following search script but can only search one table column
I have the following PHP script: <?php $fortune = `fortune`; echo $fortune; ?> but
I have the following PHP code to remove special characters from a variable; <?php
I have the following code $content .= ' <div id=myDivID>'; include 'search.php'; $content .=
I'm using following script: <?php $domain_name = 'davidwinstead.com'; $domain_name = strtolower(trim($domain_name)); $yahoo_url = 'http://siteexplorer.search.yahoo.com/search?p=http%3A%2F%2F'.$domain_name;
I have the following php script which works flawlessly in normal circumstances (i.e. visiting
I have the following codes: require(class.XMLHttpRequest.php); function hot($news){ $url=https://localhost/search.aspx?search=.$news.; $ajax=new XMLHttpRequest(); $ajax->setRequestHeader(Cookie,Cookie: host); $ajax->open(GET,$url,true);
I have following url: www.example.com/index.php/search/search_data/Doctor:a/Gender:Male/Language:Urdu/ and I want to convert it to associative array
I have a PHP/mySQL search script that for some reason, after the latest update
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.