shmop_read

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

shmop_readLee datos a partir de un bloque

Descripción

shmop_read(Shmop $shmop, int $offset, int $size): string

shmop_read() lee una cadena en un bloque de memoria compartida.

Parámetros

shmop

El identificador del bloque de memoria compartida, creado por la función shmop_open()

offset

Posición desde la cual se debe comenzar a leer; debe ser superior o igual a cero e inferior o igual a la longitud real del segmento de memoria compartida.

size

El número de bytes a leer; debe ser superior o igual a cero, y la suma de offset y size debe ser inferior o igual a la longitud real del segmento de memoria compartida. 0 lee shmop_size($shmid) - $start bytes.

Valores devueltos

Devuelve los datos.

Errores/Excepciones

Si offset o size están fuera del rango, se lanza una ValueError.

Historial de cambios

Versión Descripción
8.0.0 shmop ahora requiere una instancia de Shmop en lugar de un resource.
8.0.0 Si offset o size están fuera de límite, se lanza una ValueError; anteriormente se emitía una E_WARNING y se devolvía false.

Ejemplos

Ejemplo #1 Lee un bloque de memoria compartida

<?php
$shm_data
= shmop_read($shm_id, 0, 50);
?>

Este ejemplo lee 50 bytes del bloque de memoria compartida y los coloca en $shm_data.

Ver también

add a note

User Contributed Notes 4 notes

up
7
Craig Manley
20 years ago
shmop_read() reads and returns the whole memory segment's data. This is not useful if you're just working with strings. If you need to read a string from shared memory, call str_from_mem() on the result of shmop_read(). Similarly when writing strings to memory (instead of binary data), null terminate your strings with str_to_nts() before passing the value on to shmop_write().

function str_to_nts($value) {
return "$value\0";
}

function str_from_mem(&$value) {
$i = strpos($value, "\0");
if ($i === false) {
return $value;
}
$result = substr($value, 0, $i);
return $result;
}
up
3
macmaster at pobox dot com
24 years ago
When i need to read the whole string at that shm pointer, setting the count parameter to zero (0) seems work for me.
up
2
michael dot wuertz at six dot de
4 years ago
With shmop_read, you probably get a "\0" - padded string.

$zero_padded = shmop_read($shm_seg, 0, 128);

$usable_string = rtrim($zero_padded, "\0");
up
-2
slavapl at mailandnews dot com
24 years ago
Also you can use the shmop_size() function to determine the block size.
To Top