I have a string
$content = "your image is [img]url to image.png[/img] now you can use it";
With php script I want
$content = "your image is now you can use it";
$content = "your image is [img]url to image.png[/img] now you can use it";
echo preg_replace("/\[img\](.+?)\[\/img\]/i", '', $content);
Output:
your image is now you can use it
If there is a single-instance of [img][/img], you can use a combination of substr() and strpos():
$first = substr($content, 0, strpos($content, '[img]'));
$end = substr($content, strpos($content, '[/img]') + 6);
$content = $first . $end;
If there can be multiple instances within the same string, you'll need to put it in a loop:
$openImg = strpos($content, '[img]');
while ($openImg !== false) {
$first = substr($content, 0, $openImg);
$end = substr($content, strpos($content, '[/img]') + 6);
$content = $first . $end;
$openImg = strpos($content, '[img]');
}
Related
i would like to find array of bytes in opened file and replace it with another one.
Any idea how to do that ? :)
This is how i read something from the file, everything works.
$current_file = basename($_FILES['fileToUpload']['name']);
$someInfoFromFile = 0;
if(isset($_POST["submit"]))
{
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $current_file))
{
$fh = fopen($current_file, 'rw');
fseek($fh, 1000);
$someInfoFromFile = fread($fh, 2);
fclose($fh);
echo "Bytes read from file: " . $someInfoFromFile;
}
}
example:
Find $Buffer1 = array(10, 30, 30, 10);
Replace it with $Buffer2 = array(20, 20, 20, 20);
Maybe return if found or not.
str_replace() is binary safe, just make strings from your bytes. You can do this manually using the chr() function:
$search = chr(10) . chr(30) . chr(30) . chr(10);
$replace = chr(20) . chr(20) . chr(20) . chr(20);
Or build it from your array:
$search = pack('C*', ...$buffer1);
$replace = pack('C*', ...$buffer2);
Or maybe:
$search = join('', array_map('chr', $buffer1));
$replace = join('', array_map('chr', $buffer2));
Then:
$oldContents = file_get_contents($file); // or however you get it
$newContents = str_replace($search, $replace, $oldContents, $count);
echo "$count instances were replaced\n";
$url = 'MyUrl';
$contents = file_get_contents($url);
function scrape_between($data, $start, $end){
$data = stristr($data, $start);
$data = substr($data, strlen($start));
$stop = stripos($data, $end);
$data = substr($data, 0, $stop);
return $data;
}
$svetaines_turinys = trim(scrape_between($contents, "<table border=\"0\" cellspacing=\"0\">", "</table>"));
$fp = fopen("autogidas.php", "w+");
fwrite ($fp, "$svetaines_turinys");
fclose ($fp);
$fh = fopen("autogidas.php", 'r') or die("negalima atidaryti");
while(! feof($fh)) {
$visa_data1 = fgets($fh);
$visa_data = trim($visa_data1);
$pavadinimas = trim(scrape_between($visa_data, "<span class=\"ttitle2\">", "</span>"));
$metai = trim(scrape_between($visa_data, "<span class=\"ttitle1\">", "</span>"));
$kaina = trim(scrape_between($visa_data, "<span class=\"ttitle1\" style='float: left;'>", "<br /><span class=\"grey\">"));
echo "$pavadinimas<br> $metai <br> $kaina . <br><br>";
}
fclose($fh);
Output is working fine, but the problem is the output with a lot of free space, I tried to use trim(), but it didn't solved the problem.
You could just use regex to accomplish this task, something like this will work perfectly:
$metai = preg_replace('/\s+/', ' ',scrape_between($visa_data, "<span class=\"ttitle1\">", "</span>"));
Just do it on every var with the same problem.
If you mean you want to remove multiple space and just leave a single space you could use str_replace() like this
function scrape_between($data, $start, $end){
$data = stristr($data, $start);
$data = substr($data, strlen($start));
$stop = stripos($data, $end);
$data = substr($data, 0, $stop);
return str_replace(' ', ' ', $data);
}
$find = '{<p>something</p>}';
$str1 = "<p>{<p>something</p>}</p>\r\ntext<p>something else</p>";
// or
$str2 = "<p>something</p>\r\n{<p>something</p>}aa<p>t</p>\r\ntext<p>something else</p>";
Basically, $find can be anywhere in the string. New lines delimiter is "\r\n".
I need to find $find in the $str and remove specific html tags around $find in that specific string line. No tags should be removed from $find.
Expected output would be
// For $str1
$str1 = "{<p>something</p>}\r\ntext<p>something else</p>";
// For $str2
$str2 = "<p>something</p>\r\n{<p>something</p>}aat\r\ntext<p>something else</p>";
The string might be very long, so no regex solutions please.
What I have figured out:
$pos = strpos($str, $find);
if ($pos !== false) {
$contentLength = strlen($str);
$lineStart = (int)strrpos($str, "\r\n", -$contentLength+$pos); // cast false to 0 (start of string)
$lineEnd = strpos($str, "\r\n", $pos);
if ($lineEnd === false)
$lineEnd = strlen($str);
$lineLength = $lineEnd-$lineStart;
if ($lineLength < 0)
return;
var_dump(substr($str, $lineStart, $lineLength));
}
Which dumps that specific line in the string.
My final solution:
function replace($find, $str, $replace) {
$pos = strpos($str, $find);
if ($pos !== false) {
$delim = "\r\n";
$contentLength = strlen($str);
$lineStart = strrpos($str, $delim, -$contentLength+$pos);
if ($lineStart === false)
$lineStart = 0;
else
$lineStart += strlen($delim);
$lineEnd = strpos($str, $delim, $pos);
if ($lineEnd === false)
$lineEnd = strlen($str);
$lineLength = $lineEnd - $lineStart;
$line = substr($str, $lineStart, $lineLength);
$posLine = strpos($line, $find); // Where $find starts
$findLength = strlen($find);
$line = substr_replace($line, '', $posLine, $findLength); // Remove $find from $line
$begin = replaceTags(substr($line, 0, $posLine));
$end = replaceTags(substr($line, $posLine));
return substr_replace($str, $begin.$replace.$end, $lineStart, $lineLength);
}
}
function replaceTags($str) {
return str_replace(array('<p>', '</p>'), '', $str);
}
echo replace($find, $str, $replace);
I'm currently using the following code from this tutorial to automatically split the WordPress post content in to 2 columns. However, how would I change this code to output 3 columns instead of only 2?
functions.php code:
function content_split($text, $separator = '<hr/>', $start = false ) {
if ( $start === false) {
$start = strlen($text) / 2;
}
$lastSpace = false;
$split = substr($text, 0, $start - 1);
// if the text is split at a good breaking point already.
if (in_array(substr($text, $start - 1, 1), array(' ', '.', '!', '?'))) {
$split .= substr($text, $start, 1);
// Calculate when we should start the split
$trueStart = strlen($split);
// find a good point to break the text.
} else {
$split = substr($split, 0, $start - strlen($separator));
$lastSpace = strrpos($split, ' ');
if ($lastSpace !== false) {
$split = substr($split, 0, $lastSpace);
}
if (in_array(substr($split, -1, 1), array(','))) {
$split = substr($split, 0, -1);
}
// Calculate when we should start the split
$trueStart = strlen($split);
}
//now we know when to split the text
return substr_replace($text, $separator, $trueStart, 0);
}
index.php code:
<div class="first-column my-column">
<?php $text = get_the_content(); $separator = '</div><div class="second-column my-column">'; echo apply_filters('the_content', content_split($text,$separator)); ?>
</div>
function content_split($text, $separator = '<hr/>') {
$string = '';
$start = ceil(strlen($text) / 3);
$string.= substr($text,0,$start);
$string.= $separator;
$string.= substr($text,$start,$start);
$string.= $separator;
$string.= substr($text,($start*2),$start);
return $string;
}
I have got a string and would like to remove everything after a certain "dot"+word combination. For instance:
This.Is.A.Test
=> would become
This.Is.A
Were you looking to remove everything after a specific dot+word, or just remove the last dot+word? If you're looking for a specific word, try this:
$str = "This.Is.A.Test";
$find = ".A";
$index = strpos($str, $find);
if ($index !== false)
$str = substr($str, 0, $index + strlen($find));
echo $str; // "This.Is.A"
In response to #SuperSkunk:
If you wanted to match the whole word, you could do this:
$find = ".A.";
$str = "This.Is.A.Test";
$index = strpos($str, $find);
if ($index !== false)
$str = substr($str, 0, $index + strlen($find) - 1);
echo $str; // "This.Is.A"
$str = "This.Is.AB.Test";
$index = strpos($str, $find);
if ($index !== false)
$str = substr($str, 0, $index + strlen($find) - 1);
echo $str; // "This.Is.AB.Test" (did not match)
$str = "This.Is.A.Test"; $str = substr($str, 0, strrpos($str, "."));
$result = explode('.', $str, 4);
array_pop($result);
implode('.', $result);
I'll do something very simple like :
<?php
$string = 'This.Is.A.Test';
$parts = explode('.', $string);
array_pop($parts); // remove last part
$string = implode('.', $parts);
echo $string;
?>
$pos = strpos($haystack, ".A" );
$result = substr($haystack,0,$pos);
...something like this.