PHP hyperlinks generator - HELP plz

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
monicka87
Forum Newbie
Posts: 2
Joined: Thu Oct 22, 2009 12:18 pm

PHP hyperlinks generator - HELP plz

Post by monicka87 »

Hi

I need some help to get this done using php:


1 - I have few hyperlinks say 500 in format like:

<a href="http://domaina.com/1.html" target="_blank">http://domaina.com/1.html</a>
<a href="http://domainb.com/1.html" target="_blank">http://domainb.com/1.html</a>
<a href="http://domainc.com/21.html" target="_blank">http://domainc.com/21.html</a>
<a href="http://domaind.com/new.php" target="_blank">http://domaind.com/new.php</a>

etc etc

Now I want to convert them into format like:

<a href="http://domaina.com/1.html" target="_blank">keyword 1</a>
<a href="http://domainb.com/1.html" target="_blank">keyword 2</a>
<a href="http://domainc.com/21.html" target="_blank">keyword 3</a>
<a href="http://domaind.com/new.php" target="_blank">keyword 4</a>


Here keyword 1,2,3,4 can be taken randomly from a file which has huge list of keywords or may be we can paste the hyperlinks and keywords in php form itself and then it just links into the manner as given above.

Can anyone provide the code for doing this?

Thanks
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: PHP hyperlinks generator - HELP plz

Post by superdezign »

Provide code? The pattern you are going for is very unclear. But, I'm bored. I'll give it a stab.

Code: Select all

$keywords = array('keyword 1', 'keyword 2', 'keyword 3', 'keyword 4');
$randomString = '&&&&&&&';
$content = preg_replace('~(<a href=".+?" target="_blank">).*?(</a>)~', '$1' . $randomString . '$3', $content);
 
$position = 0;
foreach ($keywords as $keyword) {
  if (($position = strpos($content, $randomString, $position) !== FALSE) {
    $content = substr_replace($content, $keyword, $position, strlen($randomString));
  } else {
    break;
  }
}
Replaces all the target areas with a random string, searches for that string, and replaces it with the next keyword in your list. It does exactly what you asked for, and until you show any work on your part, I'm not adding anything to it.
monicka87
Forum Newbie
Posts: 2
Joined: Thu Oct 22, 2009 12:18 pm

Re: PHP hyperlinks generator - HELP plz

Post by monicka87 »

Thanks.. I am revising my problem and making it more clear and simple now as:

I would need a PHP form page with two form fields:

Form Field A-

In this box we can paste bulk URLS like

http://domaina.com/1.html
http://domainb.com/2.html
http://domaind.com/my.html
http://domaina.com/new.html
etc


Form Field B-

In this box we can paste list of keywords like

keyword 1
keyword 2
keyword 3
keyword 4
keyword 5
etc

Lastly there is "SUBMIT" button and when we click on it then it provides us code on the page itself in the following manner:

<a href="http://domaina.com/1.html">keyword 1</a>
<a href="http://domainb.com/2.html">keyword 2</a>
<a href="http://domaind.com/my.html">keyword 3</a>
<a href="http://domaina.com/new.html">keyword 4</a>
etc

There is no database or external files required..


I hope its easy to understand now..


If anyone can create this script then I would be very grateful..

Thanks
Monicka
Post Reply