1

I have a string as below

String

<span class="post-excerpt"> - <a href="./posts/the-post-title">17 posts</a> - Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilite de un nov lingua franca: On refusa continuar payar custosi traductores. At solmen va esser necessi far uniform grammatica, pronunc</span>

Now I want to remove - from string (not from the URL within the string)

I have tried to use str_replace() but than it is removing from URL also and that resulting broken links of course.

Anyone can please help me to remove - from the string but not from the URL

Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • @who-gave-negative-vote Thanks for the negative vote :) without even write few words about what is wrong in the question. – Code Lover Jul 30 '14 at 07:55
  • You can make something like regex to find all - between > < that way you will only replace all - in your string without change the url – nicearma Jul 30 '14 at 07:56
  • But the problem is that the system what I am working on is containing `span` tag also in string. I know it is stupid but it is like that. BTW, It's third party open source system – Code Lover Jul 30 '14 at 08:00

4 Answers4

1

Assuming that the string will always be in that format, you can alter your str_replace to be more specific, thus ignoring the - in the URLs:

$newString = str_replace('> - <', '><', $oldString);

Like I said, ensure the format is always the same, i.e > - <

Alex
  • 1,565
  • 2
  • 9
  • 13
  • This works partially, but as you see in my string. There are two `-` apart from URL – Code Lover Jul 30 '14 at 08:10
  • I have done using `str_replace()` with `array` and it is working fine. – Code Lover Jul 30 '14 at 08:22
  • Great. Good luck with your project – Alex Jul 30 '14 at 08:25
  • @CodeLover while you may have got this to work, this kind of approach is very fragile to minor changes in your HTML and is likely to catch you out at some point. You're better off parsing HTML properly using the correct tools. See my answer, for example. – Tom Fenech Jul 30 '14 at 10:46
  • @TomFenech I agree with you. This may create an issue if something changes in the string. I have tried your solution (seems look more promising) but somehow didn't work in first try. Let me try once again. – Code Lover Jul 31 '14 at 03:42
1

You could use DOMDocument, which will parse your HTML. This means that you can use str_replace only on the contents of your elements, rather than risk modifying their attributes as well.

It looks a lot more long-winded but it's also a lot safer and will still continue to work if the format of your HTML changes slightly in the future:

$html = '<span class="post-excerpt"> - <a href="./posts/the-post-title">17 posts</a> - Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilite de un nov lingua franca: On refusa continuar payar custosi traductores. At solmen va esser necessi far uniform grammatica, pronunc</span>';

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

// DOMDocument creates a valid HTML document, adding a doctype, <html> and <body> tags
// The following two lines remove them
// http://stackoverflow.com/a/6953808/2088135
$doc->removeChild($doc->firstChild);
$doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);

$span = $doc->getElementsByTagName('span')->item(0);    
foreach ($span->childNodes as $node) {
    $node->nodeValue = str_replace(' - ', '', $node->nodeValue);
}

echo $doc->saveHTML();

Output:

<span class="post-excerpt"><a href="./posts/the-post-title">17 posts</a>Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilite de un nov lingua franca: On refusa continuar payar custosi traductores. At solmen va esser necessi far uniform grammatica, pronunc</span>
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
0

Not elegant, but working and universal method:

1) replace all - occurences inside href attribute with some predefined "word" - combination of characters which not include -. This can be done by preg_replace_callback.

2) Do plain string replacement by str_replace:

$result = str_replace('-', '', $source);

3) Do backward replacement of all "word" occurences with - character.

hindmost
  • 7,125
  • 3
  • 27
  • 39
-4
$newString = str_replace('> - <', '><', $oldString);
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Jitendra
  • 558
  • 8
  • 23
  • 2
    This answer is exactly the same as [Alex's one](http://stackoverflow.com/a/25031693/2088135), except that you haven't used a code block or provided any explanation of what it does. – Tom Fenech Jul 30 '14 at 10:49