1

I'm trying to use preg_replace to strip out a section of code but I am having problems getting it to work right.

Code Example:

$str = '<p class="code">some string here</p>';

PHP I'm using:

$pattern = array();
$pattern[0] = '!<p class="code">!';
$pattern[1] = '!</p>!';
preg_replace($pattern,"", $str);

This strips out the code just as I want with the exception of the space between the p and class.

Returns:

  some string here //notice the single space at the beginning.

I'm trying to get:

some string here //no space at the beginning.

I have been beating my head against the wall trying to find a solution. The reason I'm trying to strip it out in a chunk instead of breaking the preg_replace into pieces is because I don't want to change anything that may be in the string between the tags. Any ideas?

David
  • 11
  • 1

5 Answers5

1

That does not happen for me (and it shouldn't).

It may be a space output somewhere else (use var_dump() to view the string).

alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks for the comment. I wish it was that simple but I tried trim() and it not only trimmed the space at the beginning of the string but all other spaces in the string. I just want that first space trimmed and no other spaces. – David Jul 20 '11 at 03:53
  • @David `trim()` only strips trailing and leaving whitespace. – alex Jul 20 '11 at 03:55
  • Alex you were right I tried my code on a clean page with no css and so on and the beginning space is missing so I am picking up that space from somewhere else. Thanks for the help. – David Jul 20 '11 at 04:09
0

You might want to look into this thread to see if you want to switch to using DOMDocument. It'll save you a great deal of headaches trying to parse through HTML.

Robust and Mature HTML Parser for PHP

Community
  • 1
  • 1
Edgar Velasquez Lim
  • 2,426
  • 18
  • 15
0

test:

<?php
$str = '<p class="code">some string here</p>';

$pattern = array();
$pattern[0] = '!<p class="code">!';
$pattern[1] = '!</p>!';
$result = preg_replace($pattern,"", $str);
var_dump($result);

result:

php pregrep.php                                                                  
string(16) "some string here"

seems to work just fine.

Richard Hoffman
  • 729
  • 3
  • 10
0

Alex I figured out where I was picking up the extra space.

I was putting that code into a text area like this:

$str = '<p class="code">some string here</p>';
$pattern = array();
$pattern[0] = '!<p class="code">!';
$pattern[1] = '!</p>!';
$strip_str = preg_replace($pattern,"", $str);


<textarea id="code_area" class="syntaxhl" name="code" cols="66" rows="5">
 <?php echo $strip_str; ?>
</textarea>

This gave me my extra space but when I changed the code to:

<textarea id="code_area" class="syntaxhl" name="code" cols="66" rows="5"><?php echo $strip_str; ?></textarea>

No line spaces or breaks the extra space went away.

David
  • 11
  • 4
0

Why not use trim()?

$text = trim($text);

This removes white spaces around strings.

casraf
  • 21,085
  • 9
  • 56
  • 91