Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8441829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:40:25+00:00 2026-06-10T08:40:25+00:00

I import some text from an XML file and I trim it and replace

  • 0

I import some text from an XML file and I trim it and replace multiple white spaces.

$var = $myxmltext;
$var = trim($var);
$var = preg_replace('/\s+/',' ',$var);

For some reason I get “raw html” like this when I echo it:

quot; or IÂ’ve instead of I've

Any ideas why?

Here is my trim function:

function mytrim($mytrim){
    $mytrim = utf8_decode($mytrim); 
    $mytrim = trim($mytrim);
    $rule1 = array(
        ",",    // virgula
        ".",    // punct
        "~",    // ~
        "_",    // underscore
        "-",    // liniuta
        ")",    // paranteza inchidere
        ":",    // doua puncte
        ">",    // mai mare
        "<",    // mai mic
        "!",
        "?",
        "*",
        "&"
    );
    $rule2 = array(
        ", ",   // virgula
        ". ",   // punct
        " ~ ",  // ~
        " ",    // underscore
        " - ",  // liniuta
        ") ",   // paranteza inchidere
        ": ",   // doua puncte
        " > ",  // mai mare
        " < ",  // mai mic
        "! ",
        "? ",   
        "* ",
        " & "
    );
    $mytrim = str_replace($rule1, $rule2, $mytrim);
    $rule3 = array(
        " .",   // punct
        " ,",   // virgula
        " ?",   // question mark
        " !",
        " *",
        " )"
    );
    $rule4 = array(
        ".",    // punct
        ",",    // virgula
        "?",    // question mark
        "!",
        "*",
        ")"
    );
    $mytrim = str_replace($rule3, $rule4, $mytrim);
    $mytrim = preg_replace('/\s+/',' ',$mytrim);
    return $mytrim;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:40:27+00:00Added an answer on June 10, 2026 at 8:40 am

    Try this regex before you do you stuff:

    preg_replace('/(&)\s+(\w+;)/', '$1$2', $text);
    

    Then do your business, lets see if that HTML encodes right now.

    So what this will do is solve your main problem of HTML encoding by changing all:

    & quote;
    

    to:

    &quote;
    

    Be aware: This might not work exactly as expected so please test.

    Of course as others say you can also utf8_decode/encode as well to get rid of those umlet characters.

    Edit

    To solve the Ampersand problem try:

    preg_replace('/&(?!\w+;)/', ' & ', $text);
    

    So this will replace all & that is not in the form &quote; and give them a space either side.

    Same as normal, test it first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code to retrieve XML data: import cStringIO import pycurl from xml.etree
I got an XML file from 3rd party that I must import in my
I'm trying to use Nokogiri to grab some data from an XML file, then
I have to import some XML data into my app. Now I open a
I am running some import code asynchronously from a simple WinForms app using a
I try to parse an xml file. The text which is in tags is
Using the following code, I push data from an xml file into a classic
I have some xml: <article> <uselesstag></uslesstag> <topic>oil, gas</topic> <body>body text</body> </article> <article> <uselesstag></uslesstag> <topic>food</topic>
I'm creating an import script using Nokogiri to parse an XML file and then
My application is fetching data from a Xml file on a webpage. That date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.