kayra Posted August 26, 2020 Share Posted August 26, 2020 Hi, as a newbee i'm trying to figure this code out: $search = get_the_title(); $lines = file('links.txt'); // Store true when the text is found $found = false; foreach($lines as $line) { if(strpos($line, $search) !== false) { $found = true; echo $line,"<br/>"; } } // If the text was not found, show a message if(!$found) { echo 'not found'; links.txt file contains links like: <a href="https://blabla.com/blablabla/blablablabla/The word or file to search" target="_blank" rel="noopener noreferrer">The word or file to search</a> I just want to search the this part: ( ">The word or file to search</a> ) of the url then return it as an clickable url and also limit search results to 3. Could somebody help me which code and where to put in php code above? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/311396-searching-after-last-slash/ Share on other sites More sharing options...
gw1500se Posted August 26, 2020 Share Posted August 26, 2020 (edited) I suggest you are going about it the wrong way. Use DOM instead. Edited August 26, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/311396-searching-after-last-slash/#findComment-1580992 Share on other sites More sharing options...
kayra Posted August 26, 2020 Author Share Posted August 26, 2020 (edited) Hi, thank you for your reply, i'm a newbee and just want to figure out the php code above. Edited August 26, 2020 by kayra Quote Link to comment https://forums.phpfreaks.com/topic/311396-searching-after-last-slash/#findComment-1580994 Share on other sites More sharing options...
Phi11W Posted August 27, 2020 Share Posted August 27, 2020 As given, the code loops through each line in the file, looks to see if that line contains the entered word and, if so, displays that line on screen, effectively creating the HTML link (because that's what each line of the file is!). It's a really clumsy way of doing this, because The links are hard-coded in the file, making them difficult to maintain, and The test (strpos) will find the given word anywhere in line, so if you were to enter the value "target", it would match each and every line! A simpler and safer way might be to hold just the words in the file, without all the HTML stuff, and search that, then create the HTML for the selected line: . . . $found = false; foreach ($lines as $line) { if ($line === $search) { $found = true; printf( '<a href="https://blabla.com/blablabla/blablablabla/%s" target="_blank" rel="noopener noreferrer">%s</a><br/>', $line, $line ); break; } } // If the text was not found, show a message if (!$found) . . . Regards, Phill W. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311396-searching-after-last-slash/#findComment-1581005 Share on other sites More sharing options...
kayra Posted August 28, 2020 Author Share Posted August 28, 2020 Hi Phil, thank you for your reply and help. Does the code you wrote searches after the last "/" (slash)? Because all the urls are different in each line in links.txt file, when i look at the code above i see it searches only one url: https://blabla.com/blablabla/blablablabla/%s Is it true? Best regards. Quote Link to comment https://forums.phpfreaks.com/topic/311396-searching-after-last-slash/#findComment-1581023 Share on other sites More sharing options...
Phi11W Posted September 1, 2020 Share Posted September 1, 2020 On 8/28/2020 at 9:38 PM, kayra said: Does the code you wrote searches after the last "/" (slash)? No. It doesn't do anything at all with slashes. As I said: Quote A simpler and safer way might be to hold just the words in the file, without all the HTML stuff, and search that So your file might look like this ... Quote apple banana catfish dogfish ... and, if you entered "apple", the code would generate the link: <a href="https://blabla.com/blablabla/blablablabla/apple" target="_blank" rel="noopener noreferrer">apple</a><br/> If you are expecting multiple links to be generated based on what you enter, then please explain how you would expect this to work. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/311396-searching-after-last-slash/#findComment-1581070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.