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
https://forums.phpfreaks.com/topic/128188-solved-need-help-string-replace/
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**

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.

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

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;
?>

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

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

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> ";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.