Jump to content

How to display only part of an URL address - Help please thanks


newtothis
 Share

Recommended Posts

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

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

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

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

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

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 by davej
Link to comment
Share on other sites

 

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...