Jump to content

Searching after last slash


kayra

Recommended Posts

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.

Link to comment
Share on other sites

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 

  1. The links are hard-coded in the file, making them difficult to maintain, and 
  2. 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.

 

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 filewithout 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.

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.