Jump to content

explode not working....


doa24uk

Recommended Posts

Hi guys,

 

Here's what I've got so far ....

 

$message = "<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>";
$messageimp = explode (">", $message,1);
for($i = 0; $i < count($messageimp); $i++){
echo $messageimp[$i];
}

 

All I want to do is chop $message down so the all I've left with is

 

http://www.mysite.com/?d=9FDS32S

 

 

Is this possible given that there are double quotes in the original variable??

Link to comment
Share on other sites

well first of all, that code is rife with syntax errors. when you have double quotes in a string, you need to escape them, IE

 

$string = "quotes\" and quotes\" and quotes\"";

 

Additionally, explode will leave with you 2 chucks

 

<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank"

and

http://www.mysite.com/?d=9FDS32S</a

 

I would suggest using a regex function to extract whats between the tags. That would probably be the easiest way to get just the URL

Link to comment
Share on other sites

Or instead escaping use single quotes, that allows you to use double quotes inside string without escaping. Better for html since there is much more double quotes usually in it. And imo its more readable.

 

<?php
$message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>';

 

Edit: only thing if you want variables to echo somewhere among the string or want special characters to appear like new lines for example then use double. Still variables could be done with string concencation with single quotes.

Link to comment
Share on other sites

Or instead escaping use single quotes, that allows you to use double quotes inside string without escaping. Better for html since there is much more double quotes usually in it. And imo its more readable.

 

<?php
$message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>';

 

Edit: only thing if you want variables to echo somewhere among the string or want special characters to appear like new lines for example then use double. Still variables could be done with string concencation with single quotes.

 

Ah yes, thats probably the easier way to do it. I never liked doing it that way, for various irrational reasons.

 

Also, here is a good tutorial on regex

http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html

Link to comment
Share on other sites

 

$message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>';

$value=explode('target="_blank">', $message);
$value=explode("</a>", $value[1]);
$value = trim($value[0]);

echo $value;

Link to comment
Share on other sites

 

$message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>';

$value=explode('target="_blank">', $message);
$value=explode("</a>", $value[1]);
$value = trim($value[0]);

echo $value;

 

this assumes everyone of his links has the target attribute set _blank, has the target attribute as the last set attribute, that it has double quotes, has no space between it and the ">" character, etc. Regex would be a lot more appliable to different types of links with different attributes

Link to comment
Share on other sites

Of course it does....and this assumes this is a link

 

$message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>';

$value=explode("</a>", $message);
$value=explode(">", $value[0]);
$value = trim($value[1]);

echo $value;

Link to comment
Share on other sites

That way it's not very dynamic. What if you use like this <a href="something.php">DOWNLOAD</a> , then it fails.

 

<a href="something.php">DOWNLOAD</a>  will become DOWNLOAD

 

See my second post

 

Yes I am aware of that... but I think OP was looking for to get the url what is assigned to href attribute in the link.

 

Edit:

Nevermind.. now that I reread the original post. There is same value in link and in href. So yours will do just fine with that. I might have been totally on the wrong tracks..

Link to comment
Share on other sites

Apparently its the same thing:

 

 href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>

 

in this case yes ... in all cases ... maybe not.

 

that is why people are trying to give him a solution that works in ALL cases

 

just explode the href=" part to get the url

 

Now say after me

 

$message = 'Regex is the best ! All hail explode';

$value=explode("!", $message);
$value = trim($value[1]);

echo $value;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.