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
https://forums.phpfreaks.com/topic/254798-find-integer-in-string/
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.

  <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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.