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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.