I’m trying to remove non-RFC characters after filtering a URL with other methods. This method breaks:
$query = 'www.example.com_-.su_-.1Mk8ij_-.www.cs.cmu.edu_-.~mjw_-.recipes_-.cheese_-.cheese-garlic-biscuits.html';
$query = preg_replace('/([^a-zA-Z0-9._-])/e', 'sprintf("_-%2.2x", ord($1))', $query);
The error returned is
Failed evaluating code: \nsprintf("_-%2.2x", ord(~))
It breaks on other examples as well and I can’t figure out why. Can anyone point me in the right direction?
Because the
$1is getting filled in already due to variable interpolation, and thus you’re trying to callord(~)instead oford("~"). Use"$1"instead of$1.