Need some suggestions for the methodology to achieve the following:
My current script gets text and if it has URL(s) then it replaces them. The issue is I want to truncate the URLS(s) so they do not break the width of a table or unsightly line break to fit them.
$text = file_get_contents("temp.txt");
$link = preg_replace('#(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)#', '$1', $text);
echo $link;
I am concerned that if I substr() the $link then it won't work if multiple URLs are found. Can you PHP the $1 in the replacement? Any alternatives?
Use preg_replace_callback to modify the match and replacement. This returns the first 10 characters as an example:
$link = preg_replace_callback('#(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)#',
function($m) {
return ''.substr($m[1], 0, 10).'';
},
$text);
This kind of problem can also be solved on client side using css (I assume you are speaking about the html element table in your question).
To do that, you have to give your cell a fixed size and to set the display property to inline-block. Then you can define the behaviour of the cell when a word is too long using the white-space, overflow and text-overflow properties.
Example:
<html>
<head>
<style>
.mytable td:nth-child(2) {
display: inline-block;
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<table class="mytable">
<tr><td>abcd</td><td>www.smallurl.jp</td><td>efgh</td></tr>
<tr><td>ijkl</td><td>www.a-giant.url/larger/than/the/cell/width</td><td>mnop</td></tr>
</table>
</body>
</html>
Related
The HTML looks like this:
$html = 'SOME TEXT<p style="border-top: 0.0px;border-right: 0.0px;vertical-align: baseline;border-bottom: 0.0px;color: #000000;padding-bottom: 0.0px;padding-top: 0.0px;padding-left: 0.0px;margin: 0.0px;border-left: 0.0px;padding-right: 0.0px;background-color: #ffffff;">SOME TEXT';
I tried strip_tags($html, '<p>'); to remove everything except for <p> but that preserves all the style elements of the tag.
I want the above to be replaced with just <p>
What's the best approach?
Thanks!
The simplest solution for this would be something based on preg_replace().
$html = 'SOME TEXT<p style="border-top: 0.0px;border-right: 0.0px;vertical-align: baseline;border-bottom: 0.0px;color: #000000;padding-bottom: 0.0px;padding-top: 0.0px;padding-left: 0.0px;margin: 0.0px;border-left: 0.0px;padding-right: 0.0px;background-color: #ffffff;">SOME TEXT';
$html = strip_tags($html, '<p>');
$html = preg_replace('/\sstyle=["\'][A-Za-z0-9-:\s.;#]{1,}["\']/', '', $html);
As always, you should always be somewhat careful when trying to parse html with regex. For instance, this would fail if for some reason the text inside the <p /> tag contained something formatted like a css style. (Something like <p>If I typed style="color:red" inside the tags, it would also be removed</p>)
The next step to make something like this better would be to actually parse the string as an XML document using the DOMDocument class. It depends on how robust a feature set you are looking to achieve. However, this method could change your string in unexpected ways; for instance, parsing your string as a DOMDocument would cause a </p> tag to be added. That kind of validation may or may not be useful for you.
also some time very useful will remove style tag like below
`$html = '<style>li {
list-style: none;
background: url(/images/Articles/ordasoft_discount.png) no-repeat;
padding-left: 30px;
}
li p{font-size:16px}
ul li {
padding-left: 30px;}
.portfolio-container{width: 45%;}
</style> Hello word ';`
this will help:
`$html = preg_replace('/<style>[A-Za-z0-9-:\s.\/_;#\(\)\{\}%]{1,}<\/style>/', '',$html) ;`
i got another problem removing linebreaks from within a token in a template file - the template i have to parse could look something like this:
<html>
<style>
body { color: black }
div {
background-color:#fff;
}
<!-- i need to remove the line breaks within {_WP_token[*]} for parsing //-->
<h4>{_WP_token[id="42";class="foo"; style="border:4px solid
green;padding:20px"; onclick="(this.value=confirm('foo'));"]}</h4>
<script>
function() {
console.log('my foo is my castle');
}
</script>
I tried myself without making the break-through. i just succeeded in creating a greedy one, which eats up the first half of the token - here it is:
preg_replace("/(\{_(.*?)(.*\s))/ix", "[LB REMOVED]", $htmlTemplate);
returns
<html>
<style>
body { color: black }
div {
background-color:#fff;
}
<!-- it just ate up the first half of my token ! //-->
<h4>[LB_REMOVED]green;padding:20px"; onclick="(this.value=confirm('foo'));"]}</h4>
<script>
function() {
console.log('my foo is my castle');
}
</script>
i made a fiddle here: http://www.phpliveregex.com/p/2EZ
thank you very much in advance.
best regards,
Lupo
Try:
$newString = preg_replace("/[\n\r]/","[LB_REMOVED]", $originalString);
I am not sure I understand you correctly. If you want to change
<h4>{_WP_token[id="42";class="foo"; style="border:4px solid
green;padding:20px"; onclick="(this.value=confirm('foo'));"]}</h4>
into
<h4>{_WP_token[id="42";class="foo"; style="border:4px solid green;padding:20px"; onclick="(this.value=confirm('foo'));"]}</h4>
Then you can use this
\{_(.*)[\r\n]+
and replace with $1, See it on Regexr
preg_replace("/\{_(.*)[\r\n]+/", "$1", $htmlTemplate);
I removed all your modifiers, since you do not use them. See Modifiers doc
I have a Database table to save news. the news field type is TEXT (I save the news with its styles inline).
Now when I show the news on the site it shows with all the styles and that is great but my problem is:
I want to make last 10 news table on mainpage will show the title and PART OF NEWS i use a php code to get some letters from my returned news value from the database:
$str1 = $cat_news[$i]['news'];
$str1 = wordwrap($str1, 100);
$str1 = explode("\n", $str1);
when I echo the $str[1]; it shows me the styles like this:
letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none;
I want to show the text without any style just some letters from the text followed by ...
There is a function in PHP called strip_tags.
You can use it like this:
$str1 = $cat_news[$i]['news'];
$str1 = strip_tags($str1);
$str1 = wordwrap($str1, 100);
$str1 = explode("\n", $str1);
This should get rid of all HTML formatting.
You could use strip_tags(), but beware it can remove more than you think if the HTML is badly formatted etc. It doesn't do proper check on the formatting etc. It might also be possible to use DOMDocument, but it really depends on how proper and complete the text and formatting is.
I'm building the backend to a web application, where it is possible to change the design of a page. Once the user has setup the design the way they like it, they should be able to save it and the new design would be written to the specific file.
For this reason, I will need to replace all the characters between { and } after a certain string, which would be the name of the class.
So a very simple concept, say the following class was in a seperate file which I load as a view, style.php. I would need to go from:
from
<style>
.bonus {
bottom: 6px;
left: 247px;
}
</style>
to
<style>
.bonus {
bottom: 20px;
left: 450px;
}
</style>
Could someone recommend me on the best way to
a) find a string in a file,
b) when that is found, replace everything between two strings right after the first string.
Thank you.
I don't like the concept of the user making changes to the actual file very much. There are a lot of safer methods by which a user could create and maintain a custom template without them actually making changes to a PHP file.
What about storing the user's CSS in a field in a database? Then you'd simply need to do something like:
<?php
$css = getCSSByUserId($userId); //function runs query on DB to get user-specific CSS
/* $css = ".bonus {
bottom: 20px;
left: 450px;
}" */
?>
<style>
<?php echo $css; ?>
</style>
If you really want to edit the actual file, you'd do it something like this:
<?
$file = "/path/to/file.php";
//The user's replacement CSS
$replace = '.bonus {
bottom: 20px;
left: 450px;
}';
$str = file_get_contents($file);
$str = preg_replace('/\.bonus \{.*\}/U', $replace, $str);
$res = fopen($file, 'w');
fwrite($res, $str);
fclose($res);
?>
I checked the regex here http://www.quanetic.com/Regex and it works.
I thought this was going to be pretty simple, but I've been struggling with it now for a while. I know there are CSS parser classes out there that can acheive what I want to do... but I don't need 95% of the functionality they have, so they're not really feasible and would just be too heavy.
All I need to be able to do is pull out any class and/or ID names used in a CSS file via regex. Here's the regex I thought would work, but hasn't.
[^a-z0-9][\w]*(?=\s)
When run against my sample:
.stuffclass {
color:#fff;
background:url('blah.jpg');
}
.newclass{
color:#fff;
background:url('blah.jpg');
}
.oldclass {
color:#fff;
background:url('blah.jpg');
}
#blah.newclass {
color:#fff;
background:url('blah.jpg');
}
.oldclass#blah{
color:#fff;
background:url('blah.jpg');
}
.oldclass #blah {
color:#fff;
background:url('blah.jpg');
}
.oldclass .newclass {
text-shadow:1px 1px 0 #fff;
color:#fff;
background:url('blah.jpg');
}
.oldclass:hover{
color:#fff;
background:url('blah.jpg');
}
.newclass:active {
text-shadow:1px 1px 0 #000;
}
It does match most of what I want, but it's also including the curly brackets and doesn't match the ID's. I need to match the ID's and Classes separately when conjoined. So basically #blah.newclass would be 2 separate matches: #blah AND .newclass.
Any ideas?
===================
FINAL SOLUTION
I wound up using 2 regex to first strip out everything between { and }, then simply matched the selectors based on the remaining input.
Here's a full working example:
//Grab contents of css file
$file = file_get_contents('css/style.css');
//Strip out everything between { and }
$pattern_one = '/(?<=\{)(.*?)(?=\})/s';
//Match any and all selectors (and pseudos)
$pattern_two = '/[\.|#][\w]([:\w]+?)+/';
//Run the first regex pattern on the input
$stripped = preg_replace($pattern_one, '', $file);
//Variable to hold results
$selectors = array();
//Run the second regex pattern on $stripped input
$matches = preg_match_all($pattern_two, $stripped, $selectors);
//Show the results
print_r(array_unique($selectors[0]));
[^a-z0-9][\w]+(?=\s)
I changed your * to a + match
It works fine in RegEXR - an awesome regex development tool: http://gskinner.com/RegExr/ (See bottom right of window to download the desktop version)
This version is based on nealio82's, but adding pseudo-selectors:
[^a-z0-9][\w:-]+(?=\s)
/(?<!:\s)[#.][\w]*/
some thing like this? excludes the #FFFFFF color stuff...
The solution posted by OP works, though it didn't work for me with CSS classes that had hyphens. As such, I've amended the second pattern to work more effectively:
$pattern_two = '/[\.|#]([A-Za-z0-9_\-])*(\s?)+/';