PHP - Help Finding A Matching Word Within A Phrase Of Text. Using A String.
Hi, and thanks for any help with this.
I have this but of php i have been trying relentlessly to get working.. for over a month. <?php $abs="one two three four words"; $mT = "testing for matching words"; $words=explode(' ', $abs); //$sf=($words); if (preg_match($words, $mT)) { echo "the url $mT contains a Word"; } else { echo "the url $mT does Not contain a Word"; echo "$words[4]"; } ?>
You see, I get a responce only using the last line "$words[4]", (obviously, because I am pointing it the Matching word, (ie: with [4]) Edited June 9, 2020 by x1705 better description. Similar TutorialsFolks, I want to find a Word in a string. If the word is found, i want to retrun TRUE else FALSE. $string: Sol 3 Drawer Chests Antique Pine Bedside Chest $word to be found: Chest Now the tricky part is: In the $string the $word occurs twice once its CHESTS (i mean the first occurance) then CHEST. If its found, i want to return TRUE else FALSE. I tried using strpos() but it just checks for CHEST and not CHESTS. How can it be achieved? Cheers Natasha for example, i want to search for an the string "example@example.com" and erase it. thanks in advance !
Hi, simply put;
$abs="Bob Rob Sue";
how can I put into code, when "Bob1:2" is used, it finds a match of "Bob", and assigns the complete string of "Bob1:2" ? <?php $abs="Bob Rob Sue"; $abby="sometext Bob1:2 is Sue3:4 where Rob2:1 etc"; $nums = preg_replace('/[a-zA-Z]/', ' ', $abby); $lets = preg_replace('/[0-9]:?[^0-9]/', ' ', $abby); //$lets1=array_intersect($abs, $lets); $absArr = explode(' ', $abs); $abbyArr = explode(' ', $lets); $letsx=array_intersect($absArr, $abbyArr); //echo $letsx[0] . "</br>"; //############################################# $nums1='/(\d+\:\d+)/s'; preg_match_all($nums1, $nums, $nums2); foreach($nums2 as $numsx){ } $abi0="$letsx[0]+$numsx[0]"; $abi1="$letsx[1]+$numsx[1]"; $abi2="$letsx[2]+$numsx[2]"; $answer=$abi0."</br>".$abi1."</br>".$abi2."</br>"; echo $answer; ?>
current results: Here we go, I need to use a query where it uses a posted time value to compare if there are the same times on the posted date value. I want it so the user cant book the same time on the same day as someone before bascially. My input so far is this Code: [Select] <?php ob_start();?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Conforming XHTML 1.0 Strict Template</title> <link rel="stylesheet" type="text/css" href="style.css" /> <link type="text/css" href="ui-lightness/jquery-ui-1.8.16.custom.css" rel="Stylesheet" /> <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script> </head> <body> <form name="input" action="input.php" method="post"> Subject: <input type="text" name="subject" /> First Name: <input type="text" name="firstname" /> Surname: <input type="text" name="surname" /> Trainer: <input type="text" name="trainer" /> Email: <input type="text" name="email" /> Date: <input type="text" name="event_date" id="date" /> Time: <input type="text" name="event_time" id="time" /> <input type="submit" value="Submit" name="submit" /> </form> <script type="text/javascript"> $('#date').datepicker(); $('#time').timepicker({}); </script> <?php include_once("functions/database.php"); include_once("functions/number.php"); if (isset($_POST["submit"])) { echo $_POST['event_date']; echo mdy2mysql($_POST['event_date']); echo $_POST['event_time']; echo time2mysql($_POST['event_time']); $queryselect = "SELECT * FROM events LIKE '".$_POST['event_time']."'"; if ($queryselect == true) { echo "sorry this time is already booked"; } else { $query = "INSERT INTO events (subject, firstname, surname, trainer, email, event_date, event_time, status) VALUES('".$_POST["subject"]."', '".$_POST["firstname"]."', '".$_POST["surname"]."','".$_POST["trainer"]."','".$_POST["email"]."' ,'".mdy2mysql($_POST['event_date'])."','".time2mysql($_POST['event_time'])."', 'pending' ) "; $result = mysql_query($query, $db_link) or die(mysql_error().'cannot get results!'); header("Location: input.php"); } ?> can anyone help me ? very much appreciated. Hello All, I've been knocking my head against the wall for several hours trying to figure out what's going on with a simple string replace. For the most part it is working beautifully, but if a string contains a similar but not exact match it still replaces the non match. I was under the impression that str_replace always looks for an exact match. I have custom tags that are in strings of text that are structured like: [A_CUSTOM_HEADER] [A_CUSTOM_TITLE] and so on. They always start with "[A_CUSTOM" and end with a " ] ". In between the start and end can be just about anything. So my problem is if I have [A_CUSTOM] and [A_CUSTOM_TAG] in the same text str_replace seems to incorrectly replace all the [A_CUSTOM_TAG] with the [A_CUSTOM] content and the match never happens for the [A_CUSTOM_TAG] content. For example: $content = 'Here is some text and [A_CUSTOM]. Here is more text and [A_CUSTOM_TAG]'. $content = str_replace('[A_CUSTOM]','I have a and custom',$content); $content = str_replace('[A_CUSTOM_TAG]','I have a, custom and tag',$content);So running that code The [A_CUSTOM_TAG] content never gets replaced correctly, it is replaced on the first pass with the [A_CUSTOM] replacement content. I hope this make sense I'm trying to explain it the best I can. I tried using preg_replace() with \b to match the whole word but didn't get anything close to the respected results...haha I'm sure I'm missing something simple...or at least I hope I am. Thanks in advance! Edited by Twitch, 22 May 2014 - 11:52 AM. I am looking for a date within larger string, lets say the date is December 4, 2010. To find it I use pattern and function below: $Pattern='/[(January|February|March|April|May|June|July|August|September|October|November|December)] \d, \d\d\d\d/i'; preg_match_all($Pattern, $String, $Matches, PREG_OFFSET_CAPTURE, $NumberPosition); The function finds the dates within the string but to my supprise the result I get in $Matches is: r 4, 2010 What I would like to get is: December 4, 2010 but don't know how it should be fixed. I thought that with the pattern I am using but obviously that is not the case. I need to know if a string exists within a string. I've tried using STRIPOS() but it returns zero or false if what I'm looking for is the first part of the string. I don't care what function I use, but I need to get a "true" when the needle string is present anywhere in the haystack string, even at location zero. Is there some way to set up the STRIPOS() statement? I've tried: if ((stripos($title, 'needle') > 0 ) if ((stripos($title, 'needle') = true ) and even if ((stripos($title, 'needle') >= 0 ) which returns true for everything, even when the needle string is NOT present. Any help appreciated. thanks, Tom I have a web server which contains a public site and a private site. I want to be able to test for public or private using the page title and return the appropriate header which contains a different banner, css etc. Firstly - the syntax below seems to be incorrect because it doesnt work correctly. More importantly how do I add all the values to an array and than iterate through it to check for a matching page title? Code: [Select] public function publicSite() { if ($this->getTitle()== 'Home' || 'About Us'||'Registration' || 'Sitemap' || 'Contact Us' || 'Useful Links' || 'Feedback') { return true; } return false; } Then later on I would have: Code: [Select] if ($this->publicSite()) { require($ROOT.'interface/pages/publicheader.php'); } else { require($ROOT.'interface/pages/privateheader.php'); } Thanks heaps! I am trying to find a character in the follwing string $string = ' Mumbai Mast Kallander - 02 - Mumbai Mast Kallander'; the character i want to find is '-' and the find how many times does it occur in the string.. can anyone help? This topic has been moved quickly to PHP Regex because I need to go to work instead of lurking phpfreaks. http://www.phpfreaks.com/forums/index.php?topic=359460.0 %3$s displays a string of text that always begins with a number followed by a colon (from 1-25 only). 1: 2: 3: etc... Is there a way to remove the number and the colon entirely and leave the rest of the string intact? Code: [Select] function wp_rss( $url, $num_items = -1 ) { if ( $rss = fetch_rss( $url ) ) { if ( $num_items !== -1 ) { $rss->items = array_slice( $rss->items, 0, $num_items ); } //start count $i = 1; foreach ( (array) $rss->items as $item ) { printf( '<div title="%2$s"><strong>'.$i.'</strong></div><div>%3$s</div>', esc_url( $item['link'] ), esc_attr( strip_tags( $item['description'] ) ), htmlentities( $item['title'] ) ); //increment $i++; } Ashley I have a DB of data: article-id, section-id, subsection-id, article-text. When a row is fetched I am displaying article, section, subsection as <h3>, <h4>, <h5> and then text in a <p>. Before outputting the text I want to scan it for other articles, sections, or subsections it may reference then turn that reference into a hyperlink. I read the PHP documentation but I can't find anything that translates to the basic commands of instring, indexof, and left/mid/right. Example: 1. Article Name 100. Article 1, section 1 100.a article 1, section 1, subsection a Text blah blah blah 105.f blah blah blah
I want to find that 105.f and turn it into a link. All my articles, sections, and subsections are in the DB so I know I can use an array of the number values as my items to look for, but I can't figure out how to look. I want to catch the first word of a string in which the words are separated by "-" (e.g. "first-word-second-one-more"). I used this code: $string = explode('-', $string); echo $string[0]; The problem is that when the string contains only one word, it returns null Whats the quickest way to find a word in a string and wrap <b> tags around it? Code: [Select] $search = "php web"; $striing = "PHP is the web scripting language of choice"; Basically im looking for the output to be : "<b>PHP</b> is the <b>web</b> scripting language of choice" There are many ways to delete everything after a character in a string. I am looking for a simple way to delete everything after a given word. I have been becoming more and more familiar with PHP over the past few months, but this problem stumps me. How can I take a string of words separated by spaces taken from an XML feed (example: word1 word2 word3 word4) and turn it into something like this: <a href="word1">word1</a> <a href="word2">word2</a> ... and so on. This has to be done on the fly, since it is taking the words from the XML feed, they change depending on the page. I want them to be in a vertical column, and be able to link each one to the search page for that word, so each link has to be different. An even more in-depth problem, how can I sort it then so that there are multiple columns if necessary, say one page only has 3 words, but another has 50. How can I split them up into multiple vertical columns, say with 10 words in each? Have I confused anyone yet? How could I split a string in half but not in the middle of a word? $information = "This is the string of text, however at some point in the sentence a word may get split in half."; $cut = round(strlen($information)/2); ///where to cut (50%) $split1 = substr($information, 0, $cut); //first half $split2 = substr($information, $cut); // second half i am making a search engine to search my site and the code is going well but i don't know how to find your search within the results and put it in bold (like google does). I use a thing called http://simplehtmldom.sourceforge.net/ for getting the html of my website. Code: [Select] <?php include('simple_html_dom.php'); ?> <?php $s = $_GET["s"]; if ($s == null) echo "you have not searched for anything"; else echo "you searched for $s<hr>"; $urls[0] = 'http://url.com/'; $urls[1] = 'http://url2.com'; foreach ($urls as $url) { $html = file_get_html($url); strip_tags($html); preg_match("/<body>(.*)<\/body>/s", $html, $body); $htmls_split = split('<', $body[1]); foreach ($htmls_split as $line) { if (preg_match("/$s.*/", $line, $matches)) { echo "<p>$matches[0]"; } } } ?> |