I have this code: $var = "This is my website <a href=\"index.php\">My website</a> and I want to see your opinion"; echo $var; I want to look in the variable $var and search for <a . When I find this <a I to take what is between this <a and </a> and put it into a different variable, $var2. Can someone, please, tell me how can I do this?
This may work: <?php $var = "This is my website <a href=\"index.php\">My website</a> and I want to see your opinion"; preg_match('/<a(.*?)<\/a>/', $var, $goodies); $var2 = $goodies[1]; echo $var2; ?> PHP:
Regex would work, as well as using a complicated mixture of built in functions. Use strpos first to see if there is an anchor at all in the var your checking... if(strpos($var, '<a') !== false) { // Run regex here for match } Code (markup):
<?php $var = "This is my website <a href=\"index.php\">My website</a> "; $out=explode("<a",$var); $out2 = explode("</a>",$out[1]); $result = out2[0]; ?> PHP: