Jump to content

find integer in string


egnjohn

Recommended Posts

I am making a online Bible with search function. I was able to do the boolean search for text keywords without much trouble. Now I want a user to be able to type in an exact verse into search form and have that verse display in results.

 

IE: User types in John 3:16 and search result displays that verse. The following works if the user types John:3:16. I cannot figure out how to do this without requiring the user to type a : after book name.

if($search_type=='verse'){	

$query2 = explode(":", $query);
$bookquery = $query2[0];
$chapterquery = $query2[1];
$versequery = $query2[2];

$result4 = $db->sql_query("select * FROM Bible$version WHERE book = '$bookquery' AND chapter= '$chapterquery' AND verse ='$versequery'");
while ($row = $db->sql_fetchrow($result4)) {
$book = $row['book'];
$chapter = $row['chapter'];
$verse = $row['verse'];
$scripture = $row['scripture'];

echo "<b><a href=\"modules.php?name=Bible&call=chapter&viewbook=$book&viewchapter=$chapter&version=$version\">$book</a> $chapter:$verse</b>";
echo "<br>";
echo "$scripture";
echo "<hr>";
}

if(!$query){
echo "<b>You did NOT enter any keywords.</b><br>";	
}
if(!$scripture){
echo "<b>No results found.</b>";	
}

}

}

 

Is there a function to find first integer in the string and insert a : before it?

Link to comment
Share on other sites

Usually when doing a search the values are broken up by spaces, or by each $_POST or $_GET value and not a colon.

And since using a colon is common for what you are doing, I feel it's a bad mix.

 

But I see Muddy's code works for you.

 

Alternately you can make a form breaking up these 3 values and use the post input

 

a very simple example

<form action="" method="POST">
<input type="text" name="book" value="<?php echo $_POST['book'];?>" placeholder="book name" />
<input type="text" name="chapter" value="<?php echo $_POST['chapter'];?>" placeholder="chapter number" />
<input type="text" name="verse" value="<?php echo $_POST['verse'];?>" placeholder="verse number" />
<input type="submit" value="Search" />
</form>

 

You should be checking for empty or incorrect values, like if is an integer or not, and also escaping the users input before the mysql query.

Link to comment
Share on other sites

  <form method="post">
    Search: <input type="input" name="search" maxlength="40" />
    <input type="submit">
  </form>
<?php
  $book=$chapter=$verse=NULL;
  if(isset($_POST['search']) && !empty($_POST['search']))
  {
    $search=urldecode($_POST['search']);
    preg_match('@(\w*)?\s*(?\d+)\d+))?@',$search,$match);
    $book=(!empty($match[1]))?$match[1]:NULL;
    $chapter=(!empty($match[2]))?$match[2]:NULL;
    $verse=(!empty($match[3]))?$match[3]:NULL;
  }
  $ok=TRUE;
  foreach(array('book','chapter','verse') as $term)
  {
    if(empty($$term))
    {
      echo "Please enter $term<br />".PHP_EOL;
      $ok=FALSE;
    }
  }
  if(!$ok) die();
  echo "Search in book ($book) $chapter:$verse";
?>

[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.