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
Monicka

Recommended Answers

All 8 Replies

Do the links change according to the keywords or do the links remain the same while keywords are switched around?

Thanks for your reply.

Links and keywords both can change..

It would be good if I can get a form with two boxes where I can paste hyperlinks and keywords and it generates the code 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>

Thanks
Monicka

Member Avatar for diafol

This sounds like a job for Regexman. Up, up and away!

However, the form is easy.

What do you want to do with the generated link? Store it in a file or place it in a DB??

<form ...>
...
<input type="text" id="url" name="url" />
...
<input type="text" id="label" name="label" />
...
<input type="text" id="desc" name="desc" />
...
</form>

Then...

<?php
$url = clean($_POST['url']);   //where clean() is your custom data cleaning function 
$label = clean($_POST['label']);
$desc = clean($_POST['desc']);

$output = "<a href='$url' title='$desc' target='_blank'>$label</a>";

//then print out $output wherever you want or place it in a file or DB.
?>

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


I hope its easy to understand now..


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

Thanks
Monicka

Member Avatar for diafol

Well, I thought I had given you the bare bones. You've got to do some of the work yourself.

1. Replace the input widgets with textarea widgets. Ask users to place each url on a new line in the box and search for a "newline" (carriage return).
2. "Explode" the $_POST variable with '\n' to get an array of urls.
Do same with $_POST. See the php online manual on 'explode()'.
3. Check to see same number of elements in each array (now called $urls and $labels respectively). Is count($urls) == count($labels)?
4. Build the output thus:

$i = 0;
while($i < count($urls)){
  $output .= "<a href='{$urls[$i]}' target='_blank'>{$labels[$i]}</a><br />"
  $i = $i + 1;
}

5. echo the $output variable in the appropriate place.

You can do what ardav says like below -

<?php
//use this Defined array if wanna put keywords directly here
//$keywords = array('key1', 'key2','key3','key4','key5','key6','key7');
if(isset($_POST['btn']) && $_POST['btn']!='')
{
	$links_arr = explode(',',$_POST['A']);
	
	$link = array();
	$keywords = explode(',',$_POST['B']);
	for($i=0;$i<count($links_arr);$i++)
	{
		$elem = "<a href=".$links_arr[$i].">".$keywords[rand(0,count($links_arr))]."</a>";
		array_push($link,$elem); 
	}
	
}
//then print out $output wherever you want or place it in a file or DB.
?>
<html>
<head></head>
<body>
<form id="frm" name="frm" method="post">
<textarea id="A" name="A" rows="5" ></textarea>
<textarea id="B" name="B" rows="5" >
</textarea>
<p id="show"><?php for($i=0;$i<count($link);$i++){ echo $link[$i]; echo "<br>";} ?>
 </p>
<? //echo count($link); ?>
<input type="submit" name="btn" id="btn" value="Submit" onclick=""  />
</form>
</body>
</html>

Just put the links and the keywords in the comma seperated forms

I tried this code in php file but after I paste and urls and keywords in respective boxes and submit, it outputs nothing?

You can do what ardav says like below -

<?php
//use this Defined array if wanna put keywords directly here
//$keywords = array('key1', 'key2','key3','key4','key5','key6','key7');
if(isset($_POST['btn']) && $_POST['btn']!='')
{
	$links_arr = explode(',',$_POST['A']);
	
	$link = array();
	$keywords = explode(',',$_POST['B']);
	for($i=0;$i<count($links_arr);$i++)
	{
		$elem = "<a href=".$links_arr[$i].">".$keywords[rand(0,count($links_arr))]."</a>";
		array_push($link,$elem); 
	}
	
}
//then print out $output wherever you want or place it in a file or DB.
?>
<html>
<head></head>
<body>
<form id="frm" name="frm" method="post">
<textarea id="A" name="A" rows="5" ></textarea>
<textarea id="B" name="B" rows="5" >
</textarea>
<p id="show"><?php for($i=0;$i<count($link);$i++){ echo $link[$i]; echo "<br>";} ?>
 </p>
<? //echo count($link); ?>
<input type="submit" name="btn" id="btn" value="Submit" onclick=""  />
</form>
</body>
</html>

Just put the links and the keywords in the comma seperated forms

Member Avatar for diafol

Play with it - you've got a good bit of code with which to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.