mishasoni Posted September 27, 2008 Share Posted September 27, 2008 Hello: First off, I am a PHP newbie. I have a database field called 'ExpertiseWeb' that contains web addresses. I am trying to append "http://" to any records that do not contain the http:'' prefix, but I am having no luck. It either returns nothing, only the http://, or http://http:// followed by the field data. The code is below. Please help! <?php if (!empty($row_rsParticipants['ExpertiseWeb'])) { // checks "ExpertiseWeb" field to see if it is empty if (substr('$url',0,7) != 'http://') { // adds "http://" prefix if none exists $url = "http://".$url; } elseif (substr('$url',0,7) == 'http://') { $url = $row_rsParticipants['ExpertiseWeb']; } }?> <?php if (!empty($row_rsParticipants['ExpertiseWeb'])) { // Show if field not empty ?> <tr> <td height="17" align="left" valign="top" bordercolor="0"><strong>Website:</strong></td> <td align="left" valign="top" bordercolor="0"> <a href="<?php echo $url ?>"><?php echo $url ?></a> </td> </tr> <?php } // Show if field not empty ?> Link to comment https://forums.phpfreaks.com/topic/126018-concatenation/ Share on other sites More sharing options...
xtopolis Posted September 27, 2008 Share Posted September 27, 2008 I prefer preg_match. Here's some logic you can implement into your code. Hint, replace the substr functions. Try it, and if you have trouble post what you tried, and I will help you some more <?php $ar = array('http://www.xtopolis.com','xtopolis.com','www.xtopolis.com','https://nonstop.xtopolis.com','',''); $c = count($ar); for($i=0;$i<$c;$i++) { $prefix = 'http://'; $pattern = '/^http(s)?\:\/\//'; //Starts with http (has, or has not an 's') and also :// if(empty($ar[$i])){ echo 'No url given.<br >'; continue; }//empty? echo no url, skip if(!preg_match($pattern,$ar[$i])){ // match? no, add prefix, yes, leave it alone $url = $prefix.$ar[$i]; }else{ $url = $ar[$i]; } echo $url.'<br />'; } /*//outputs http://www.xtopolis.com http://xtopolis.com http://www.xtopolis.com https://nonstop.xtopolis.com No url given No url given */?> Link to comment https://forums.phpfreaks.com/topic/126018-concatenation/#findComment-651659 Share on other sites More sharing options...
mishasoni Posted September 30, 2008 Author Share Posted September 30, 2008 Worked great. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/126018-concatenation/#findComment-654087 Share on other sites More sharing options...
discomatt Posted September 30, 2008 Share Posted September 30, 2008 Your method was bang on, and is actually quite a bit faster than the regex solution provided... You simply bunged up on the variables a bit <?php if (!empty($row_rsParticipants['ExpertiseWeb'])) { // checks "ExpertiseWeb" field to see if it is empty # You had # substr('$url',0,7) != 'http://' # This is wrong because for one, you're checking the literal string '$url' and not the variable # $url. You'd do this with # substr($url,0,7) != 'http://' # But even that's wrong, cause as of right now, $url is empty if (substr($row_rsParticipants['ExpertiseWeb'],0,7) != 'http://') { // adds "http://" prefix if none exists $url = "http://".$row_rsParticipants['ExpertiseWeb']; } # Same story here elseif (substr($row_rsParticipants['ExpertiseWeb'],0,7) == 'http://') { $url = $row_rsParticipants['ExpertiseWeb']; } } ?> Link to comment https://forums.phpfreaks.com/topic/126018-concatenation/#findComment-654138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.