sillysillysilly Posted February 24, 2009 Share Posted February 24, 2009 I would like to put a piece of copied HTML into a string to perfrom a preg_match on it. The problem I have is that the " used inside the HTML cause problems. below is an example of the html. If I use the ' then at some point another ' appears from the original HTML and messes things up. Ofcourse there are lots of " so there is no way I know to present this all into a string without editing it. $string= '<DIV id="richBorder"> <TABLE cellspacing="0" cellpadding="0" width="100%" border="0"> <TR> <TD><table width="98%" align="center"> <tr> <td><table width="100%" cellspacing="0"> <tr> <td width="65%"><a id="PropertyHeader_propertyNameLink" class="subtitleXLarge" href="http://www.gables.com" target="_blank">Gables West Avenue</a> <a id="PropertyHeader_mapit" href="javascript:__doPostBack('PropertyHeader$mapit','')" style="font-weight:bold;">(Map It)</a></td>" Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/ Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 Escape the single quotes: <?php $string = '<DIV id="richBorder"> <TABLE cellspacing="0" cellpadding="0" width="100%" border="0"> <TR> <TD><table width="98%" align="center"> <tr> <td><table width="100%" cellspacing="0"> <tr> <td width="65%"><a id="PropertyHeader_propertyNameLink" class="subtitleXLarge" href="http://www.gables.com" target="_blank">Gables West Avenue</a> <a id="PropertyHeader_mapit" href="javascript:__doPostBack(\'PropertyHeader' . $mapit . '\',\'\')" style="font-weight:bold;">(Map It)</a></td>'; EDIT: Something is flaking out and it is not posting correctly =\ but the above should work as long as you make sure to put the single quote before <DIV id= portion at the beginning. EDITEDIT: Error fixed, the above should work right. Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770614 Share on other sites More sharing options...
sillysillysilly Posted February 24, 2009 Author Share Posted February 24, 2009 how do you escape the single quote? Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770616 Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 addslashes() Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770617 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 \' add a \ before the single quote that needs escaping. Do not use addslashes it is not the solution. (I believe it is also going to be depreciated in PHP 6 but not 100% sure on that.) It is not, magic quotes are. Sorry for the confusion on that portion. EDIT: In the code I posted, if it wouldnt mess up like it did, should be correct. Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770620 Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 oops sorry i did not know that ! but why are they depreciating it? it is useful, do they have any alternative/improved function for it? Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770621 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 but why are they depreciating it? it is useful, do they have any alternative/improved function for it? I think that was mal-information on my part. I thought they were doing it with magic quotes, but just magic quotes will be depreciated from what I gathered. Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770629 Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 but why are they depreciating it? it is useful, do they have any alternative/improved function for it? I think that was mal-information on my part. I thought they were doing it with magic quotes, but just magic quotes will be depreciated from what I gathered. so addslashes() is safe to use ? Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770635 Share on other sites More sharing options...
sillysillysilly Posted February 24, 2009 Author Share Posted February 24, 2009 Maybe I am missing something. Even defining the string it ends due to one or the other quote. I tried \' beginning it but that did not work. I would like to say something like. $string = " hello y'all this is a " "; for example If I put ' "hello y'all.... the appostraphe quote ends the string; therefore I can not get the whole value into the string. Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770647 Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 $string = " hello y'all this is a \" "; Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770658 Share on other sites More sharing options...
SuperBlue Posted February 25, 2009 Share Posted February 25, 2009 You can use the Heredoc Syntax to do it easily, like below. $str = <<<EOD <DIV id="richBorder"> <TABLE cellspacing="0" cellpadding="0" width="100%" border="0"> <TR> <TD><table width="98%" align="center"> <tr> <td><table width="100%" cellspacing="0"> <tr> <td width="65%"><a id="PropertyHeader_propertyNameLink" class="subtitleXLarge" href="http://www.gables.com" target="_blank">Gables West Avenue</a> <a id="PropertyHeader_mapit" href="javascript:__doPostBack('PropertyHeader$mapit','')" style="font-weight:bold;">(Map It)</a></td> EOD; And you can use String Concatenation. I.e. $str = '<DIV id="richBorder"> <TABLE cellspacing="0" cellpadding="0" width="100%" border="0"> <TR> <TD><table width="98%" align="center"> <tr> <td><table width="100%" cellspacing="0"> <tr> <td width="65%"><a id="PropertyHeader_propertyNameLink" class="subtitleXLarge" href="http://www.gables.com" target="_blank">Gables West Avenue</a> <a id="PropertyHeader_mapit" href="javascript:__doPostBack(' . "'" . 'PropertyHeader$mapit' . "'" . ','')" style="font-weight:bold;">(Map It)</a></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770662 Share on other sites More sharing options...
sillysillysilly Posted February 25, 2009 Author Share Posted February 25, 2009 thanks, String concat won't really work since I have no control of the source code and do not want to go editing each and every html input. I will try the EOD thing. As you can tell I am a novice at this. I know what i need and you guys really help me to put it all together. Quote Link to comment https://forums.phpfreaks.com/topic/146781-solved-how-to-put-html-into-a-string-problem-with-quotes/#findComment-770668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.