Hi

I am creating some files of which are taken from database fields, the problem is the field in the database has £ in which is great as it is needed when in the browser page. The problem I have is when making the file is that it creates the newfile name as £ in the url rather than £. How do i replace PART of the field ['nightmare'] to make £ rather than $pound; please.

The line with the problem is

$newfile = ''. $file['whatever'].'/'. $file['whatever'].'-'. str_replace(' ','-',trim($file['nightmare'])) . '.php';

Thanks for any help

Dani AI

Generated

got it working with an array-based replace (good quick fix) and was right to ask for more context. The key points missing in the thread are encoding and safety: replacing £ with the literal currency character will work for display, but filenames and URLs are better if normalized to safe ASCII slugs to avoid cross-filesystem and browser-encoding problems.

A reliable pipeline:

  • Decode HTML entities first (so £ becomes the actual character) using html_entity_decode.
  • Transliterate non-ASCII to ASCII (with the intl extension or iconv) so special symbols become readable ASCII.
  • Replace any runs of non-alphanumeric characters with a single hyphen and trim/collapse hyphens (use a Unicode-aware preg_replace).
  • Use the result for filenames/URLs, or rawurlencode when embedding in links if you must keep non-ASCII characters.

Example slugify function (use this output for your file name rather than raw DB text):

function slugify($str) {
    $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
    if (extension_loaded('intl')) {
        $str = transliterator_transliterate('Any-Latin; Latin-ASCII', $str);
    } else {
        $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    }
    $str = preg_replace('/[^\p{L}\p{Nd}]+/u', '-', $str);
    $str = trim($str, '-');
    $str = strtolower($str);
    return $str ?: 'item';
}

Troubleshooting notes: ensure DB and connection use UTF-8, check file_exists to avoid collisions, sanitize user input before creating files, and prefer an ASCII slug (or GBP/pound) if portability is important. See transliterator_transliterate and rawurlencode for related details.

Recommended Answers

All 3 Replies

Member Avatar for Member #949455

The problem I have is when making the file is that it creates the newfile name as £ in the url rather than £. How do i replace PART of the field ['nightmare'] to make £ rather than $pound; please.

I'm bit a confused. You want someone to fixed that 1 line but never mention what is $file['whatever'] or $file['nightmare']?

Can you post a little more code? Right now it's just guessing.

Hi, firstly thanks for offering help. Basically i need to know how to do 2x string replace on the nightmare file, currently I have one in at the moment replacing space with a dash but how would I do 2 replaces please

Thanks

HI I managed to sort it by using arrays with the string replace
For anyone else who would need it

$old = array(' ', '£');
$new = array('-', '£');

$newfile = ''. $file['whatever'].'/'. $file['whatever'].'-'. str_replace($old,$new,trim($file['nightmare'])) . '.php';

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.