Jump to content

[SOLVED] Need Help, String Replace


Lamez

Recommended Posts

Ok, I want to make a new function to go along with my ELC, and I want it to search the string to see if it contains something like, www., http://www., or http:// then I want it to encrypt it(the whole URL\Link), add it to the DB(the encryption of the link), and add the regular link to the DB. Then I want it to take the link the user posted, and change the name, and link it to my ELC.

 

Any idea what functions I need to look at, besides md5(), and sha1()?

 

ELC = External Link Checker http://links.krazypicks.com

Link to comment
Share on other sites

why not replace the link with a link to your sites link checker with the DB entry ID ie

mylinkchecker.php?ID=10002

in the DB have 10002 as the ID of the record for website phpfreaks.com etc

 

to find the URLs use a REGEX replce like this:~

<?php
$HTML = "this is
a test
www.apple.com blar
tesing http://apple.com
blob http://www.apple.com hee
";
$result = preg_replace_callback('%\b((?:www\.|http://www\.|http://).*?)\s%si', 'UpdateDB', $HTML);

function UpdateDB($Add)
{
mysql_query("INSERT INTO mytable (URL) values ('$Add'')");
return "<a href=\"linkcheck.php?ID=".mysql_insert_id()."\">LINK</a>";
}
?>

 

**UNTESTED**

Link to comment
Share on other sites

cool looks useful, will this work, if I have a paragraph, with a link somewhere in the paragraph?

 

example

"Hello, my name is Awesome, you can call me cool. Here is a link to my website: www.mywebsite.com Have fun, and enjoy your day!"

 

lol, will that bit of code be able to find the link in something like that.

Link to comment
Share on other sites

ok I am getting this error:

 

 

Warning: md5() expects parameter 1 to be string, array given in /html_test.php on line 11

Check out my link: KrazyPick Link :D :d :D

 

This is the code I am using:

<?php
//DB Connection

$HTML = $_POST['html'];
$results = preg_replace_callback('%\b((?:www\.|http://www\.|http://).*?)\s%si', UpdateDB, $HTML);

function UpdateDB($add){
$id = md5($add);

mysql_query("INSERT INTO url_list (ID, URL) values ('".$id."', '".$add."')");
return "<a href=\"http://links.krazypicks.com?url=".$id."\">KrazyPick Link</a> ";
}


echo $results;
?>

 

Also in the DB,  under the URL column, all it says is Array

Link to comment
Share on other sites

have you tried hashing it instead of md5 hashing ???

 

<?php
//DB Connection

$HTML = $_POST['html'];
$results = preg_replace_callback('%\b((?:www\.|http://www\.|http://).*?)\s%si', UpdateDB, $HTML);

function UpdateDB($add){
$id = hash($add);

mysql_query("INSERT INTO url_list (ID, URL) values ('".$id."', '".$add."')");
return "<a href=\"http://links.krazypicks.com?url=".$id."\">KrazyPick Link</a> ";
}


echo $results;
?>

Link to comment
Share on other sites

 

Warning: md5() expects parameter 1 to be string, array given in /mounted-storage/home48c/sub007/sc33591-LWQU/link_check/html_test.php on line 11

KrazyPick Link

 

ok so it is in an array, but how do I take it out and form a string?

Link to comment
Share on other sites

Yeah my bad you need to use $Add[1] instead of $Add

 

<?php
$HTML = "this is
a test
www.apple.com blar
tesing http://apple.com
blob http://www.apple.com hee
";
$result = preg_replace_callback('%\b((?:www\.|http://www\.|http://).*?)\s%si', 'UpdateDB', $HTML);
echo "<br>Final Output<br>";
echo $result;

function UpdateDB($Add)
{
$id = md5($Add[1]);
$url = $Add[1];
//Add the connection stuff for the database

#mysql_query("INSERT INTO mytable (ID, URL) values ('$id','$url')");

//debug
echo "INSERT INTO mytable (ID, URL) values ('$id','$url')"."<br>\n";
return "<a href=\"linkcheck.php?ID=$id\">LINK</a>";
}
?>

 

INSERT INTO mytable (ID, URL) values ('e845cbdc3785b1a67978cbc5a146a168','www.apple.com')<br>

INSERT INTO mytable (ID, URL) values ('0f82f8155826020d153245b61160ed4e','http://apple.com')<br>

INSERT INTO mytable (ID, URL) values ('e9ea2574c49c3778f166e8b4b6ed63dd','http://www.apple.com')<br>

<br>Final Output<br>this is

a test

<a href="linkcheck.php?ID=e845cbdc3785b1a67978cbc5a146a168">LINK</a>blar

tesing <a href="linkcheck.php?ID=0f82f8155826020d153245b61160ed4e">LINK</a>

blob <a href="linkcheck.php?ID=e9ea2574c49c3778f166e8b4b6ed63dd">LINK</a>hee

 

 

*tested

Link to comment
Share on other sites

omg, I forgot to add the mysql_query, I have no idea why I forgot that.

I am having a bit of trouble finding the link in the blob of text. say I put in www.phpfreaks.com it does not pick it up at all, I have to have text before, and after the link.

 

here is the current code:

<?php
$HTML = $_POST['html'];
$result = preg_replace_callback('%\b((?:www\.|http://www\.|http://).*?)\s%si', 'UpdateDB', $HTML);
echo "<br>Final Output<br>";
echo $result;

function UpdateDB($Add)
{
$id = md5($Add[1]);
$url = $Add[1];

//DB connection

    $sql = mysql_query("SELECT * FROM `url_log` WHERE `url`='".$url."'");
    if (!mysql_num_rows($sql) >= 1){

mysql_query("INSERT INTO url_list (ID, URL) values ('$id','$url')");
    }

return "<a href=\"http://links.krazypicks.com?url=$id\">KP Link</a> ";
}
?>

test: http://links.krazypicks.com/test.php

Link to comment
Share on other sites

So I used the fix, now in the DB it inserts www., and also out puts phpfreaks.com, why?

 

I am not sure on any of this, or else I would fix it on my own.

 

code:

<?php


$HTML = $_POST['html'];
$result = preg_replace_callback('%\b((?:www\.|http://www\.|http://).*?)\s?%si', 'UpdateDB', $HTML);
echo "<br>Final Output<br>";
echo $result;

function UpdateDB($Add)
{
$id = md5($Add[1]);
$url = $Add[1];

//DB Con...


    $sql = mysql_query("SELECT * FROM `url_list` WHERE `url`='".$url."'");
    if (!mysql_num_rows($sql) >= 1){

       mysql_query("INSERT INTO url_list (ID, URL) values ('$id','$url')");

    }else{
        
      $r = mysql_fetch_array($sql);
      $id = $r['id'];    

    }

   return "<a href=\"http://links.krazypicks.com?url=$id\">Link</a> ";
}
?>

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.