0

My intention is to remove any attribute which does not have value.

Here is my Code:

<?php
$srcTxt = "
|title=
|Row18={{Timeline row
  |from=
  |to=2000
  |1-text= TYRR consolidation
  |1-at= 1904
  |2-text= TYRR and TFC takeover
  |2-at= 1927
  |3-text= private bus services acquisition
  |3-at= 1954
|Row18={{Scale row|
  |from=1840
  |to=2000
  |increment=40
  }}
}} ";

$srcTxt = preg_replace("/^ *.*= *$/m", "", $srcTxt);

echo ($srcTxt);

?>

The expected output is to remove |title= and |from= which do not have any values assigned to them.

This works perfectly here But not so when I run it locally in my system. What might be the issue?

15
  • Does it replace nothing or only the first occurence (|title=) when you run it on your system? Commented Jan 2, 2014 at 13:45
  • @BastiM Nothing changes in the output. Not even the first |title... Commented Jan 2, 2014 at 13:46
  • Just tried it and it works correctly (PHP 5.4.4-14+deb7u2). What version do you run? Commented Jan 2, 2014 at 13:47
  • @JayarathinaMadharasan: I'm unable to reproduce this issue. It works fine. Can you show the output you're getting now and the expected output? Commented Jan 2, 2014 at 13:47
  • @MikeS. My version is 5.4.19 Built: Aug 21 2013 01:07:08 Commented Jan 2, 2014 at 13:50

1 Answer 1

1
<?php
$srcTxt = "
|title=
|Row18={{Timeline row
  |from=
  |to=2000
  |1-text= TYRR consolidation
  |1-at= 1904
  |2-text= TYRR and TFC takeover
  |2-at= 1927
  |3-text= private bus services acquisition
  |3-at= 1954
|Row18={{Scale row|
  |from=1840
  |to=2000
  |increment=40
  }}
}} ";

$srcTxt = trim(preg_replace("/(.+?)=\s*\n/", '', $srcTxt));
echo $srcTxt ;

Output:

|Row18={{Timeline row
  |to=2000
  |1-text= TYRR consolidation
  |1-at= 1904
  |2-text= TYRR and TFC takeover
  |2-at= 1927
  |3-text= private bus services acquisition
  |3-at= 1954
|Row18={{Scale row|
  |from=1840
  |to=2000
  |increment=40
  }}
}}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Thanks a lot... That code works... :) But I am still curious why my code did not work.
@JayarathinaMadharasan Your snippet doesn't work on my localhost either, and I found that this issue is due to white spaces which I managed in the regex correctly. I didn't go deep to find the answer but.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.