newtothis Posted February 13, 2014 Share Posted February 13, 2014 Hello guys, I hope it is the right forum for this question. My apologies if it is not. Any help would be handsome. I have a web link extracted from a data base and I just want to display the left part of the URL before a specific character (?). I am looking for some PHP code for this. Has anyone an idea of the simplest way to do it using PHP code? The HTML code is: <a href=”<? $ firms-> firmurladdress ?>”><?= $firms->firmurladdress?></a> Instead to display: http://www.oxfordlane.com?EM= I only want to display on the browser: http://www.oxfordlane.com So only what is on the left of “?” should be displayed. Thanks a lot Link to comment Share on other sites More sharing options...
davej Posted February 14, 2014 Share Posted February 14, 2014 (edited) See the strpos() and substr() functions... http://php.net/manual/en/function.strpos.php http://www.php.net/manual/en/function.substr.php Edited February 14, 2014 by davej Link to comment Share on other sites More sharing options...
newtothis Posted February 14, 2014 Author Share Posted February 14, 2014 Thanks a lot davej, This is very helpfull. I will try to write the code using these functions and will publish the code here for any potential correction. Best regards Link to comment Share on other sites More sharing options...
newtothis Posted February 14, 2014 Author Share Posted February 14, 2014 I have written the code. It is probably not that good. I am a newbie so be kind. Thank a lot of your help, <? <a href=”<? $ firms-> firmurladdress ?>”> <? If (strops(“<?$ firms-> firmurladdress ?>”,”?”);=0;?> <?= $firms->firmurladdress?></a> <? Else: ?> <? substr(“<?$firms->firmurladdress?>,0, strops(“<?$ firms-> firmurladdress?>”,”?”));</a> <? endif; ?> ?> Would that be OK? Link to comment Share on other sites More sharing options...
Ingolme Posted February 14, 2014 Share Posted February 14, 2014 I recommend reading the W3Schools PHP tutorial, because you don't fully understand what the <? ?> tags are for. For clarity and compatibility, replace all the opening <? tags with <?php. Also, do not use <?= $var ?> as it's also causing confusion, it used to be a shorthand method for <?php echo $var ?> so use that syntax instead. Finally, once a <?php block has been opened, no more <? should appear until after a closing ?>. Link to comment Share on other sites More sharing options...
newtothis Posted February 14, 2014 Author Share Posted February 14, 2014 Thanks a lot Ingolme, It is a very big help. I have re-written the code following your remarks, I think this time is better: <?php <a href=”$ firms-> firmurladdress ”> If (strops(“$ firms-> firmurladdress ”,”?”);=0) < $firms->firmurladdress ></a> Else <substr(“$firms->firmurladdress,0, strops(“$ firms-> firmurladdress”,”?”);></a> ?> Thanks again, much appreciated. Link to comment Share on other sites More sharing options...
newtothis Posted February 14, 2014 Author Share Posted February 14, 2014 Sorry I had to post again. I had a smily face in the code. <?php <a href=”$ firms-> firmurladdress ”> If (strops(“$ firms-> firmurladdress ”,”?”);=0) < $firms->firmurladdress ></a> Else <substr(“$firms->firmurladdress”,0, strops(“$ firms-> firmurladdress”,”?”); ) ;></a> ?> Link to comment Share on other sites More sharing options...
davej Posted February 14, 2014 Share Posted February 14, 2014 (edited) Maybe something like... <?php$pos = strpos( $firms->firmurladdress ,'?');if ( $pos==false ){ $str1 = $firms->firmurladdress;}else{ $str1 = substr( $firms->firmurladdress ,0, $pos );}?><a href="<?php echo $str1; ?>"><?php echo $str1; ?></a> Edited February 14, 2014 by davej Link to comment Share on other sites More sharing options...
newtothis Posted February 14, 2014 Author Share Posted February 14, 2014 Thank you so much davej, I realize I was still very far from this. Much appreciated. Link to comment Share on other sites More sharing options...
birbal Posted February 14, 2014 Share Posted February 14, 2014 you can use http://php.net/parse_url to parse the url 1 Link to comment Share on other sites More sharing options...
thescientist Posted February 14, 2014 Share Posted February 14, 2014 Maybe something like... <?php$pos = strpos( $firms->firmurladdress ,'?');if ( $pos==false ){ $str1 = $firms->firmurladdress;}else{ $str1 = substr( $firms->firmurladdress ,0, $pos );}?><a href="<?php echo $str1; ?>"><?php echo $str1; ?></a> or a one liner, since the ternary operator is convenient shorthand syntax if your if / else only really has one statement within each block. $strl = $pos ? $firms->firmurladdress : substr( $firms->firmurladdress ,0, $pos ); Link to comment Share on other sites More sharing options...
newtothis Posted February 15, 2014 Author Share Posted February 15, 2014 Hi everyone, I see there are few ways to go around this problem. Thank you very much to all of you who have given me some help. This is much appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now