0

I have a sting that is in this format.

<span class="amount">$25</span>–<span class="amount">$100</span>

What I need to do is split that into two strings. The string will remain in the same format but the prices will change. I tried using str_split() but because the price changes I wouldn't be able to always know how many characters to split the string at.

What I am trying to get is something like this.

String 1

<span class="amount">$25</span>–

String 2

<span class="amount">$100</span>

It seems the best option I have found is to use preg_split() but I don't know anything about regex so I'm not sure how to format the expression. There may also be a better way to handle this and I just don't know of it.

Could someone please help me format the regex, or let me know of a better way to split that string.

Edit

Thanks to @rm-vanda for helping me figure out that I don't need to use preg_split for this. I was able to split the string using explode(). The issue I was having was because the '-' was encoded weird and therefore not returning correctly.

yoxalld
  • 23
  • 7
  • Try parsing it properly instead of using a regex ([see this for more info](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)) – kero Feb 23 '14 at 22:59
  • *"It seems the best option I have found is to use preg_split()"* - you need to do more research. See [Parsing Html The Cthulhu Way](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html) – Phil Feb 23 '14 at 23:00
  • Thank you for the reference material, I think it is clear that using regex on html is not a good idea. However, I don't know what my other options are at this point. – yoxalld Feb 24 '14 at 15:08

2 Answers2

3

It might be better to translate this problem into DOM:

$html = <<<HTML
<span class="amount">$25</span>–<span class="amount">$100</span>
HTML;

$doc = new DOMDocument;
$doc->loadHTML($html);

foreach ($doc->getElementsByTagName('span') as $span) {
    // do stuff with $span
    // e.g. this is how you would get the outer html
    echo $doc->saveXML($span);
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

If it always has the "-" then this would be the most simple way:

$span = explode("-", $spans); 

echo $span[0]; 

echo $span[1]; 
rm-vanda
  • 3,122
  • 3
  • 23
  • 34
  • There is always going to be a "-" so this option seems like it would work. However I can't seem to get it correct. It always returns one array item with the entire string in it. – yoxalld Feb 24 '14 at 15:11
  • When I copy+pasted your code, the "-" seems to be some other character -- (for example, ctrl+f and search for "-" - and the "-" in your code does not get hit) Just replace it in your code with "-" – rm-vanda Feb 24 '14 at 15:58
  • I tried this two different ways but couldn't get it to work. http://pastebin.com/10DB6iCN – yoxalld Feb 24 '14 at 16:14
  • Ok I will try and see what the character is that's being used, it's being created inside of a plugin that I can't edit, so hopefully I can figure it out. – yoxalld Feb 24 '14 at 16:15
  • The character is called the EN_DASH and this will solve your problem, albeit, you may be able to cut a line out if you try. http://phpfiddle.org/lite/code/u97-1c3 – rm-vanda Feb 24 '14 at 16:23
  • taken from: http://stackoverflow.com/questions/9027472/weird-dash-character-in-php – rm-vanda Feb 24 '14 at 16:27