8 Replies - 408 Views - Last Post: 19 October 2011 - 10:30 AM Rate Topic: -----

#1 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Cleaning URL entries in database

Posted 19 October 2011 - 07:42 AM

I am having a problem with url's that are entered into my database table. When a user uploads info I ask for their facebook url so when they put it in, its either www.facebook.com/myname, http://www.facebook.com/myname, facebook.com/myname, etc. And then I pull that url from my database to be displayed on a page but it doesnt always work obviously my browser is doubling up the "www." or "http://". How would I clean the field when inserted into the database so that this link always works?
	if($facebookurl == '') {
		$errmsg[] = 'Facebook URL missing';
		$errflag = true;
	}



the variable is being pulled by $_POST at the top of my script and this is the part that makes sure the field is not empty so I'm assuming it should be added on here somewhere. Is there even a way to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Cleaning URL entries in database

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Cleaning URL entries in database

Posted 19 October 2011 - 07:57 AM

You can use substr to determine what's at the beginning of the URL.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Cleaning URL entries in database

Posted 19 October 2011 - 08:02 AM

Why not just use prepared statements so you don't have to worry about cleaning your data?
Was This Post Helpful? 1
  • +
  • -

#4 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Cleaning URL entries in database

Posted 19 October 2011 - 08:08 AM

What about substr_replace and replace "http://www." or "http://" with simply "www." would that work as well? and I suppose I should start learning a little object oriented programming.. Im just not very familiar. Thank you

This post has been edited by dbeaird727: 19 October 2011 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Cleaning URL entries in database

Posted 19 October 2011 - 08:20 AM

You don't have to understand OOP in great depth to use prepared statements in PHP. It's just a little extra syntax to use them.
Was This Post Helpful? 0
  • +
  • -

#6 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Cleaning URL entries in database

Posted 19 October 2011 - 08:36 AM

Thankyou! and I'll definitely start using it where i can
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Cleaning URL entries in database

Posted 19 October 2011 - 08:38 AM

At the very least, you should drop the mysql_*() family of functions. It is no longer supported, and scheduled to be deprecated. MySQLi supports syntax that looks similar syntax to the mysql_*() functions, as well as OOP. PDO is strictly object-oriented.
Was This Post Helpful? 1
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: Cleaning URL entries in database

Posted 19 October 2011 - 09:23 AM

I don't think this is a database issue so much as it is a question of forming a proper url from what might be short hand. This is my take on doing that:
<?php
$url1 = "http://www.somesite.com/some/path";
$url2 = "www.somesite.com/some/path";
$url3 = "somesite.com/some/path";
echo generate_url($url1)."<br>";
echo generate_url($url2)."<br>";
echo generate_url($url3)."<br>";

function generate_url($theurl) {
	$replace = array("http://","www.");
	$theurl = str_replace($replace, "", $theurl);
	return $replace[0].$replace[1].$theurl;
}
?>

Was This Post Helpful? 2
  • +
  • -

#9 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Cleaning URL entries in database

Posted 19 October 2011 - 10:30 AM

Yup thats what I was looking for! Thank you! I was actually using a href=$variable and getting the variable from the database :( bad practice im sure.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1