# Why doesn't this preg\_replace work?

**URL:** <https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680>\
**Category:** General PHP Help\
**Created:** [February 12, 2011, 1:04am UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680 "2011-02-12T01:04:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [February 12, 2011, 1:04am UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680/1 "2011-02-12T01:04:24Z")

</div>

Theres something I’m missing, could someone enlighten me? For some reason it just WONT replace the spaces with dashes. The titler function works outside of the bbcode class, formats the string inside the bbcode class fine, but won’t swap out the spaces! WRYYY!  
titler.php:  
[php]\<?php  
class BBCode{   
public function parse($str) {

```
    $simple_search = array(
                            '/\[title\](.*?)\[\/title\]/is',
                            '/\[highlight\](.*?)\[\/highlight\]/is',
                            '/\[anchor\](.*?)\[\/anchor\]/is',
                            );

    $simple_replace = array(
                            $this->titler('\\1'),
                            '<span class="highlight">$1</span>',
                            '<a name="$1"></a>',
                            );
	$str = preg_replace ($simple_search, $simple_replace, $str);
    return $str;
}

public function titler($in){
	$sec = preg_replace("@\s@is", "-", $in);
    $out = '<a name="'.$sec.'" href="#'.$sec.'"><span class="title">'.$in.' - &gt;</span></a>';
	return $out;
}	

```

}  
?\>[/php]  
titler.php  
[php]\<?php  
include\_once(“titler.php”);  
$bbcode = new BBCode();  
$txt = $bbcode-\>parse("[title]this is a test[/title]some words

omg HTML

");  
echo $txt;  
?\>[/php]

thanks for reading. =]

---

<div class="post-metadata">

**Author:** ![TylerTofWA](https://www.phphelp.com/letter_avatar_proxy/v4/letter/t/f08c70/32.png) [@TylerTofWA](https://www.phphelp.com/u/TylerTofWA)\
**Post date:** [February 12, 2011, 6:19pm UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680/2 "2011-02-12T18:19:10Z")

</div>

Ran this by a few of my friends, we all googled around, maybe we’re thinking of the wrong keywords, still can’t figure it out!

---

<div class="post-metadata">

**Author:** ![Bullshizle](https://www.phphelp.com/letter_avatar_proxy/v4/letter/b/a4c791/32.png) [@Bullshizle](https://www.phphelp.com/u/Bullshizle)\
**Post date:** [February 12, 2011, 9:17pm UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680/3 "2011-02-12T21:17:37Z")

</div>

What spaces are you talking about ? 😃

---

<div class="post-metadata">

**Author:** ![TylerTofWA](https://www.phphelp.com/letter_avatar_proxy/v4/letter/t/f08c70/32.png) [@TylerTofWA](https://www.phphelp.com/u/TylerTofWA)\
**Post date:** [February 13, 2011, 12:57am UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680/4 "2011-02-13T00:57:36Z")

</div>

Yes…Context.

The spaces in the string captured from the title bbcode, which is then inserted into the tag hyphenated in the href and name, but not in the innerHTML of the tag. The hyphens do not appear as they should per the line:  
[php]$sec = preg\_replace("@\s@is", “-”, $in);[/php]

Again, the titler function WORKS (as in inserts hyphens appropriately) when called directly, but doesnt when called through as a preg\_replacement.

The anticipated output being:

```auto
<a name="this-is-a-test" href="this-is-a-test">this is a test</a>
```

:🙂

---

<div class="post-metadata">

**Author:** ![TylerTofWA](https://www.phphelp.com/letter_avatar_proxy/v4/letter/t/f08c70/32.png) [@TylerTofWA](https://www.phphelp.com/u/TylerTofWA)\
**Post date:** [February 13, 2011, 3:27am UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680/5 "2011-02-13T03:27:32Z")

</div>

by the time the string gets to the titler function, $in = \1. Meaning that the string doesnt get passed to the function and therefor cant be preg\_replaced.

I’ll find a workaround but…Can anyone answer why?

---

<div class="post-metadata">

**Author:** ![TylerTofWA](https://www.phphelp.com/letter_avatar_proxy/v4/letter/t/f08c70/32.png) [@TylerTofWA](https://www.phphelp.com/u/TylerTofWA)\
**Post date:** [February 18, 2011, 5:16am UTC](https://www.phphelp.com/t/why-doesnt-this-preg-replace-work/12680/6 "2011-02-18T05:16:44Z")

</div>

[php]$str = preg\_replace\_callback(  
‘@@is’,  
create\_function(  
‘$matches’,  
‘$split = explode(" “,$matches[1]);  
$sec = implode(”-",$split);  
return “\<a name=”$sec" href="#$sec"\>";’  
),$str);[/php]

Solved.
