0

have have a list list of jpg associated with names and I am using the replace function to remove the extension $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename); which I grabbed from another post on this site How to remove extension from string (only real extension!) . it works perfect except for people with O'Reilly or O'Neil, names that have an apostrophe in them. any help would be appreciated

Community
  • 1
  • 1
tmac
  • 37
  • 7

1 Answers1

0

Works perfectly for me, like this example:

<?php

$filename = 'O\'Reilly.jpg';
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
echo $withoutExt . "\n";

Output: O'Reilly

Try to print out the $filename before replace to ensure that is a matchable string according with the regexp.

Eduardo
  • 657
  • 1
  • 9
  • 28
  • Thanks for the input, i see what you are doing here but I am grabbing these from a list of names which would make it difficult to parse each name and if it has a ' in it place the escape character \ before it, I may have to play around with this a bit. Was hoping there was a way to do it in the regex. Thanks i'll see if I can get it to work with enumerating through a list of names... – tmac Jan 11 '16 at 21:48