egnjohn Posted January 11, 2012 Share Posted January 11, 2012 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 More sharing options...
Muddy_Funster Posted January 11, 2012 Share Posted January 11, 2012 you could try str_replace (" ", ":", $query, 1); Link to comment https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306474 Share on other sites More sharing options...
egnjohn Posted January 11, 2012 Author Share Posted January 11, 2012 I am fairly new to php, correct me if I'm wrong. This will not work on a book that has more than one word in it such as "Song of Solomon". Link to comment https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306475 Share on other sites More sharing options...
Muddy_Funster Posted January 11, 2012 Share Posted January 11, 2012 that is correct. In this case you can use $i = strrpos($query, " "); $ query=substr_replace($query, ":", $i, 1); Link to comment https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306484 Share on other sites More sharing options...
egnjohn Posted January 11, 2012 Author Share Posted January 11, 2012 Thank You so much, that works perfect! Link to comment https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306488 Share on other sites More sharing options...
QuickOldCar Posted January 11, 2012 Share Posted January 11, 2012 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 https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306493 Share on other sites More sharing options...
egnjohn Posted January 11, 2012 Author Share Posted January 11, 2012 I considered splitting values up but that would not allow a boolean text search on keywords in the scripture field. Atleast I couldn't think of a way to use one form for both types of results. Link to comment https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306509 Share on other sites More sharing options...
laffin Posted January 11, 2012 Share Posted January 11, 2012 <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 https://forums.phpfreaks.com/topic/254798-find-integer-in-string/#findComment-1306666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.