Jump to content

[SOLVED] how to put html into a string; problem with quotes


sillysillysilly

Recommended Posts

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>"

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.

\'  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.

 

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.

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 ?

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.

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>';

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.