1

I have some text that has been parsed from word, and I am trying to get the HYPERLINK section removed. I have tried the following, what am I doing wrong?

preg_replace("/(HYPERLINK "\"{2})/", "", $input_string);

Here is an example of what I would like to happen.

words words words HYPERLINK "https://stackoverflow.com" https://stackoverflow.com words words words 

Should become

words words words https://stackoverflow.com words words words 
Rizier123
  • 56,111
  • 16
  • 85
  • 130
Joel Lewis
  • 474
  • 4
  • 21

3 Answers3

2

Simply use the following pattern and replace with an empty string:

/HYPERLINK +"[^"]+" */

DEMO

PHP

preg_replace('/HYPERLINK +"[^"]+" */', "", $input_string);

EXPLANATION

  NODE                     EXPLANATION
--------------------------------------------------------------------------------
  HYPERLINK                'HYPERLINK'
--------------------------------------------------------------------------------
   +                       ' ' (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  [^"]+                    any character except: '"' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
   *                       ' ' (0 or more times (matching the most
                           amount possible))

Check also The Stack Overflow Regular Expressions FAQ to read more about regexes

Community
  • 1
  • 1
Enissay
  • 4,754
  • 3
  • 25
  • 48
1

preg replace

echo preg_replace('/HYPERLINK +"[^"]+" */', "", $input_string); // should do it

learn regular expression

explanation

  • matches the character literally
  • Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  • " matches the characters " literally
  • [^"]+ match a single character not present in the list below
  • Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  • " a single character in the list " literally (case sensitive)
  • " matches the characters " literally

old solution

$input_string = 'words words words HYPERLINK "https://stackoverflow.com" https://stackoverflow.com words words words';  

$words = explode(' ', $input_string);
foreach (array_keys($words, 'HYPERLINK') as $key) {
  unset($words[$key+1]);
}
$sentence = implode(' ', $words);

echo $sentence = str_replace('HYPERLINK ', '', $sentence);
Demodave
  • 5,292
  • 5
  • 36
  • 51
  • Leave the other non regex solution... I up voted you answer for it lol... It's always good to see more ways to solve a given problem ;-) – Enissay Jan 05 '15 at 20:40
  • And BTW you could unset both `$key` and `$key+1` so you wont need the `str_replace`... But this will work as long there's no space between the double quotes... The regex solution handles prefectly that case also – Enissay Jan 05 '15 at 20:59
0

Try something like:

preg_replace('/HYPERLINK "(.*?)"/', "", $input_string);