1

I need to build the following string:

#<tr class="[^"]*">
<td><div class="grid_content sno"><span>[^"]*</span></div></td>
<td>
<center>
<div class="grid_content2 sno">
<span>
<img src="https://seriesblanco.com/banderas/es.png">
</span>
</div>
<center>
</td>
<td>
<div class="grid_content sno">
<span>
<a href="(.*?)" target="_blank">
<img src='/servidores/powvideo.jpg' width='[^"]*' height='[^"]*' /></a>
</span>
</div>
</td>
<td>
<div class="grid_content sno"><span>[^"]*</span>
</td>
<td>
<div class="grid_content sno"><span>[^"]*</span></td>
<td></td>
</tr>#

As you can see it has "" and '' quotes and I'm finding impossible to build a string like $string = *codeabove* because it closes itself if I use one kind of quote or another, is there another one?

3 Answers3

2

You could excape a caracter with '\' so for example

$string = 'theString\''
fab
  • 1,189
  • 12
  • 21
0

Without escaping and more elegant:

<?php

$str = <<<EOD
Example of string 
using 'both' types 
of "quotes" with 
heredoc sintax.
EOD;

echo $str;

Will print:

Example of string 
using 'both' types 
of "quotes" with 
heredoc sintax.
Gabriel Souto
  • 600
  • 7
  • 19
0

Escaping the quote character is one solution. In simple cases it is the easiest thing to do.

But sometimes it is much easier to simply slip out of HTML and use the PHP interpreter instead. There is no performance hit for do so. Depending on the circumstances it leads to code that's easier to read and write. For instance...

<?php
$str = '[^"]*';
?>
<div class="grid_content sno"><span><?= $str; ?></span></div>

Admittedly a simple case where simply escaping one quote character is easy. But consider a more complicated example.

<?php
$selected = $some_value;
if($expression == true): ?>
    <div class="somecss">
        <select>
            <?php foreach($options as $key => $value): ?>
                <option value='<?= $key; ?>'
                        <?php if($key === $selected) : ?> selected = "selected"<?php endif; ?>
                        ><?= $value; ?></option>
                    <?php endforeach; ?>
        </select>
    </div>
<?php else: //$expression is false ?>
    <div class="error">No options available</div>
<?php endif; ?>
More HTML here
DFriend
  • 8,869
  • 1
  • 13
  • 26