Jump to content
Sign in to follow this  
Dubird

php help?

Recommended Posts

Ok, ran into another problem. I have a database I want to retrieve info from and format it on the page. Here's the catch: if there is a website link in that person's info, I want it to display the link, otherwise, just some text. I can't figure out what I need to do to make PHP check one certian field in a result to determine which IF-ELSE to use. I have that set up, but it's not checking, so everything is under the IF clause. (if that makes sense to others).

Any suggestions?


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites


Take this with a grain of salt as it's untested. Let me know if it works. (important: [^ -> not (\s -> space or $ -> end of line)])

 $String = preg_replace('/(http:\/\/[^\s$])/', '<a href="$1">$1</a>', $String);

It may require the select all before and after:

 $String = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1<a href="$2">$2</a>$3', $String);

Edit: Egad the colors are psychedelic in Final Fantasy theme.

Edited by Mathias

Understand this lad, fate is a fickle lady. Work with the hand you're dealt and you may just be able to run your flag up the pole. Don't, and well, you may just find your mast cut down.

Share this post


Link to post
Share on other sites

so you want it to check a person's content (ie. field that has something they inputed) for urls. If it finds a url you want php to make the url into a link so people can click on it, correct? Or are you talking about something different?

Mathias, just did the regex for you to switch urls into clickable links.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites
so you want it to check a person's content (ie. field that has something they inputed) for urls. If it finds a url you want php to make the url into a link so people can click on it, correct? Or are you talking about something different?

Mathias, just did the regex for you to switch urls into clickable links.

Pretty much, yeah. Or rather, if the field contains one thing, output one thing, (IF) and if it contains anything else, it will be a link, so it'll output something else (ELSE).

Here's what I have:

<?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC"); 

 if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

	echo("<li>" . $row["name"] . " - ");

  $site = mysql_fetch_field($result, $row['sitelink']);

	if($site = "none") {
		print ("hpage");
		}

	else {
		echo("<a href=" .$row["sitelink"]."homepage</a>");
		}

	echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>");
  }

?>

Now, this gives me no errors, but it only outputs the first clause, then goes on to the rest. I know the $site row is probally wrong, but I can't figure out what else to use there. See, that field will either have a link or nothing in it, so I just want to change that one part of the outputed HTML to reflect that.


Yesterday was the deadline for all complaints!

acsig2016.jpg

Share this post


Link to post
Share on other sites

Not sure if this will work or not. I didn't test it. I used Mathias' regex as well so who knows if it works or not. Add a few comments as well so you know what it does.

<?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC");

 if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

    echo("<li>" . $row["name"] . " - ");

  //$site = mysql_fetch_field($result, $row['sitelink']);
 // takes the $row['sitelink'] database content and checks to see if theirs anything starting with http://. If there is, then it turns it into a link.
  $site = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1<a href="$2">$2</a>$3', $row['sitelink']);

    //If there was no http:// or any content saved in the $row['sitelink'] field then we print hpage
    if($site = "") {
        print ("hpage");
        }
//else we print <a href="http://url">url</a>
    else {
  print $site;
        }

    echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>");
  }

?>


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

oops my bad I forgot to add another =

 <?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC");

 if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

    echo("<li>" . $row["name"] . " - ");

  //$site = mysql_fetch_field($result, $row['sitelink']);
 // takes the $row['sitelink'] database content and checks to see if theirs anything starting with http://. If there is, then it turns it into a link.
  $site = preg_replace('/(.*)(http:\/\/[^\s$])(.*)/', '$1<a href="$2">$2</a>$3', $row['sitelink']);

    //If there was no http:// or any content saved in the $row['sitelink'] field then we print hpage
    if($site == "") {
        print ("hpage");
        }
//else we print <a href="http://url">url</a>
    else {
  echo $site;
        }

    echo(" - " . $row["country"] . " | <b>Recieved:</b> " . $row["recieved"] . " | <B>Sent:</b>" . $row["sent"] . "</li>");
  }

?> 

</span>

Edited by gokuDX7

sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

try this I changed the regex.

&lt;?php

    // Request data

  $result = mysql_query("SELECT * FROM members ORDER BY id ASC");

 if (!$result) {
    echo("&lt;P&gt;Error performing query: " .
         mysql_error() . "&lt;/P&gt;");
    exit();
  }

  // Display the members

  while ( $row = mysql_fetch_array($result) ) {

    echo("&lt;li&gt;" . $row["name"] . " - ");

  //$site = mysql_fetch_field($result, $row['sitelink']);
 // takes the $row['sitelink'] database content and checks to see if theirs anything starting with http://. If there is, then it turns it into a link.
  $site = preg_replace('@(http?://([-\w\.]+)+(\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '&lt;a href="$1"&gt;$1&lt;/a&gt;', $row['sitelink']);

    //If there was no http:// or any content saved in the $row['sitelink'] field then we print hpage
    if($site == "") {
        print ("hpage");
        }
//else we print &lt;a href="http://url"&gt;url&lt;/a&gt;
    else {
  echo $site;
        }

    echo(" - " . $row["country"] . " | &lt;b&gt;Recieved:&lt;/b&gt; " . $row["recieved"] . " | &lt;B&gt;Sent:&lt;/b&gt;" . $row["sent"] . "&lt;/li&gt;");
  }

?&gt;


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites
Super Special Awesome! ^_^

Now, explain to me why it worked. What exactly does 'preg_replace' do and why the long string of characters? ^_^;

well theirs about 100 different ways you can do it using regular expressions (aka. regex). preg_replace takes the regex that we set and searches for that expression in whatever source we feed it. So for your site it's searching for a number of things.

1) content thats starting with http://

2) a word after http:// (1 or more)

3) a digit (if any at all)

and some other random stuff that could be stripped out but I was to lazy to do lol. This regex is over kill and could be cut down even more but since it works, don't worry about it.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

If the assumption is that the user entered a valid website, then you could have:

$site = '<a href="' . $row['sitelink'] . '">' . $row['sitelink'] . '</a>';

I'll explain a little of the regex below with a few notes. Most of it is just preference or something easily overlooked as Goku definitely knows his stuff.

I've never used the @ to replace the / required at the start and end of a php regular expression.

http://us3.php.net/manual/en/function.preg-replace.php

I usually use double quotes around my pattern as well to allow for non-printable characters such as \n (new line), \r\n (carriage return and new line), \t (tab), etc..

http://www.regular-expressions.info/characters.html

http? ~ means htt or http did you mean: https? for http or https

([-\w\.]+) ~ means literal dash, a word, or a period one or more times. (the \. could be just . since most metacharacters do not need escaped in square brackets)

Reference the character classes for a list of definitions of what characters are caught with \w and other special characters.

http://www.regular-expressions.info/charclass.html

Kelene's telling me to wrap this up, but the references at http://www.regular-expressions.info/ should prove useful.

Like Goku said, if it works it works. All of the checks shouldn't be necessary. Unfortunately, I took too long typing this much and now my ear is being pulled.

Try the following. Maybe \$. I'll eventually run some tests and let you know for sure, since I need to do something similar soon.

 $site = preg_replace("/(https?\/\/[^\s$]+)/", "<a href=\"$1\">$1</a>", $row['sitelink']);[/PHP]


Understand this lad, fate is a fickle lady. Work with the hand you're dealt and you may just be able to run your flag up the pole. Don't, and well, you may just find your mast cut down.

Share this post


Link to post
Share on other sites

http? ~ means htt or http did you mean: https? for http or https

ya, this was supposed to be for https thats why theres unneeded things in it. Like I said, I was to lazy to take them out and edit it more to her need and since it worked there wasn't really a point for me to spend more time on it since I knew you would probably view the thread and make something smaller :P.


sig.php

All hail piggy, king of bacon ^)^

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Sign in to follow this  

×
×
  • Create New...